Skip to content

Never destroy class_loader's global state - #245

Draft
mjcarroll wants to merge 2 commits into
mjcarroll/multi-loader-ownershipfrom
mjcarroll/never-destroyed-globals
Draft

mjcarroll wants to merge 2 commits into
mjcarroll/multi-loader-ownershipfrom
mjcarroll/never-destroyed-globals

Conversation

@mjcarroll

@mjcarroll mjcarroll commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Description

Fixes #33.

A ClassLoader with static storage duration (e.g. a static pluginlib::ClassLoader) is usually destroyed after class_loader's function-local statics, so its destructor uses the already-destroyed library vector, factory maps and mutexes. The globals in class_loader_core.cpp are now allocated on first use and never destroyed, which also makes the MetaObjectGraveyardVector destructor workaround unnecessary.

Is this user-facing behavior change?

No.

Did you use Generative AI?

Claude Code (Claude Opus 5.5) wrote the change; I reviewed it.

Additional Information

Stacked on #244, the other half of #33; I'll retarget this to rolling once that merges. New death test ClassLoaderStaticTest.staticLoadersDestroyedAtExit fails under ASan without the fix.

MultiLibraryClassLoader kept every ClassLoader it created in a
process-wide static vector so loaders survived while their instances
were alive. Loaders whose instances outlived the MultiLibraryClassLoader
stayed there until process exit, and without on-demand unloading their
libraries were never unloaded.

Own the loaders per MultiLibraryClassLoader instead, and have each
instance hold a shared_ptr to its ClassLoader. A loader now lives
exactly as long as its MultiLibraryClassLoader or its last instance,
and the static vector and its destruction-order workaround go away.

Signed-off-by: Michael Carroll <mjcarroll.oss@gmail.com>
A ClassLoader or MultiLibraryClassLoader with static storage duration,
such as a static pluginlib::ClassLoader, is usually created before
class_loader's function-local static globals, so it is destroyed after
them at exit. Its destructor then unloads its libraries using the
already-destroyed library vector, factory maps and mutexes.

Allocate the globals on first use and never destroy them. This also
makes the MetaObjectGraveyardVector destructor workaround unnecessary.

Fixes #33

Signed-off-by: Michael Carroll <mjcarroll.oss@gmail.com>
@mjcarroll mjcarroll self-assigned this Nov 10, 2026
@mjcarroll
mjcarroll marked this pull request as draft November 10, 2026 03:29
@mjcarroll
mjcarroll force-pushed the mjcarroll/multi-loader-ownership branch from 60da3c9 to 07a96dd Compare November 16, 2026 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant