diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 4ee770077..367b2760a 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -2372,7 +2372,10 @@ void MarkStringArray(String *inPtr, int inLength, hx::MarkContext *__inCtx) FILE_SCOPE std::mutex* sGCRootLock = nullptr; typedef std::unordered_set RootSet; -static RootSet sgRootSet; +// Never freed. Threads that are still running when the process exits, such as the thread pools of +// native libraries, remove their roots from thread-local destructors (see ThreadInfoHolder), which +// can run after the destructors of static objects. A static set would already be destroyed by then. +static RootSet *sgRootSet = new RootSet(); typedef std::unordered_map OffsetRootSet; static OffsetRootSet *sgOffsetRootSet=0; @@ -2380,13 +2383,13 @@ static OffsetRootSet *sgOffsetRootSet=0; void GCAddRoot(hx::Object **inRoot) { std::lock_guard lock(*sGCRootLock); - sgRootSet.insert(inRoot); + sgRootSet->insert(inRoot); } void GCRemoveRoot(hx::Object **inRoot) { std::lock_guard lock(*sGCRootLock); - sgRootSet.erase(inRoot); + sgRootSet->erase(inRoot); } @@ -4270,7 +4273,7 @@ class GlobalAllocator // Threads in GC free zones, or not registered at all, can still add and remove roots std::lock_guard rootLock(*hx::sGCRootLock); - for(hx::RootSet::iterator i = hx::sgRootSet.begin(); i!=hx::sgRootSet.end(); ++i) + for(hx::RootSet::iterator i = hx::sgRootSet->begin(); i!=hx::sgRootSet->end(); ++i) { hx::Object **obj = *i; if (*obj) @@ -4720,7 +4723,7 @@ class GlobalAllocator // Threads in GC free zones, or not registered at all, can still add and remove roots std::lock_guard rootLock(*hx::sGCRootLock); - for(hx::RootSet::iterator i = hx::sgRootSet.begin(); i!=hx::sgRootSet.end(); ++i) + for(hx::RootSet::iterator i = hx::sgRootSet->begin(); i!=hx::sgRootSet->end(); ++i) { hx::Object *&obj = **i; if (obj)