diff --git a/kotlin-native/runtime/src/mm/cpp/Memory.cpp b/kotlin-native/runtime/src/mm/cpp/Memory.cpp index 23620106cac..b5af13dbe83 100644 --- a/kotlin-native/runtime/src/mm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/mm/cpp/Memory.cpp @@ -113,12 +113,12 @@ extern "C" void DeinitMemory(MemoryState* state, bool destroyRuntime) { AssertThreadState(state, ThreadState::kNative); auto* node = mm::FromMemoryState(state); if (destroyRuntime) { - { - ThreadStateGuard guard(state, ThreadState::kRunnable); - node->Get()->gc().ScheduleAndWaitFullGCWithFinalizers(); - // TODO: Why not just destruct `GC` object and its thread data counterpart entirely? - mm::GlobalData::Instance().gc().StopFinalizerThreadIfRunning(); - } + ThreadStateGuard guard(state, ThreadState::kRunnable); + node->Get()->gc().ScheduleAndWaitFullGCWithFinalizers(); + // TODO: Why not just destruct `GC` object and its thread data counterpart entirely? + mm::GlobalData::Instance().gc().StopFinalizerThreadIfRunning(); + } + if (!konan::isOnThreadExitNotSetOrAlreadyStarted()) { // we can clear reference in advance, as Unregister function can't use it anyway mm::ThreadRegistry::ClearCurrentThreadData(); }