Skip to content

memory/shared: fix static destruction order fiasco via nifty-counter idiom - #884

Draft
ahmed0mousa wants to merge 1 commit into
eclipse-score:mainfrom
ahmed0mousa:ahmo_fix_static_destruction_order
Draft

memory/shared: fix static destruction order fiasco via nifty-counter idiom#884
ahmed0mousa wants to merge 1 commit into
eclipse-score:mainfrom
ahmed0mousa:ahmo_fix_static_destruction_order

Conversation

@ahmed0mousa

@ahmed0mousa ahmed0mousa commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

MemoryResourceRegistry and SharedMemoryFactoryImpl were plain Meyer singletons (MeyerSingleton::GetInstance()). ~SharedMemoryResource() accesses MemoryResourceRegistry::getInstance(), so if a client stores a std::shared_ptr in a static/long-lived context created before the registry's first use, that static could be destroyed after the registry, causing UB/crashes at shutdown.

Both singletons now use score::os::StaticDestructionGuard (the nifty-counter idiom), matching the pattern already used by score::os::Mman. The guard for MemoryResourceRegistry is pulled into the public i_shared_memory_resource.h so any client holding a static shared_ptr is protected; the guard for SharedMemoryFactoryImpl stays internal to shared_memory_factory_impl.h to keep the public shared_memory_factory.h lightweight.

Depends-on: #896

@ahmed0mousa
ahmed0mousa force-pushed the ahmo_fix_static_destruction_order branch 2 times, most recently from bb82ce6 to 9a8994c Compare August 10, 2026 12:12
@ahmed0mousa
ahmed0mousa marked this pull request as draft August 10, 2026 12:12
@ahmed0mousa
ahmed0mousa force-pushed the ahmo_fix_static_destruction_order branch from 9a8994c to 22ed784 Compare August 11, 2026 12:28
…idiom

MemoryResourceRegistry and SharedMemoryFactoryImpl were plain Meyer singletons
(MeyerSingleton<T>::GetInstance()). ~SharedMemoryResource() accesses
MemoryResourceRegistry::getInstance(), so if a client stores a
std::shared_ptr<ISharedMemoryResource> in a static/long-lived context created
before the registry's first use, that static could be destroyed after the
registry, causing UB/crashes at shutdown.

Both singletons now use score::os::StaticDestructionGuard (the nifty-counter
idiom), matching the pattern already used by score::os::Mman. The guard for
MemoryResourceRegistry is pulled into the public i_shared_memory_resource.h so
any client holding a static shared_ptr<ISharedMemoryResource> is protected; the
guard for SharedMemoryFactoryImpl stays internal to shared_memory_factory_impl.h
to keep the public shared_memory_factory.h lightweight.
@ahmed0mousa
ahmed0mousa force-pushed the ahmo_fix_static_destruction_order branch from 22ed784 to 3063470 Compare August 12, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant