diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/DestroyRuntimeMode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/DestroyRuntimeMode.kt index 21ecea9242b..e55b9955df1 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/DestroyRuntimeMode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/DestroyRuntimeMode.kt @@ -4,7 +4,7 @@ */ package org.jetbrains.kotlin.backend.konan -// Must match DestroyRuntimeMode in Runtime.h +// Must match `DestroyRuntimeMode` in Runtime.h enum class DestroyRuntimeMode(val value: Int) { LEGACY(0), ON_SHUTDOWN(1), diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index fe3e911c019..5fe3a1c36d4 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -2390,7 +2390,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: MapisMainThread) { + // If we are not destroying the runtime but we were the main thread, publish all changes to Kotlin globals. + // This `synchronize` should be before `aliveMemoryStatesCount` decrement to synchronize with + // `initMemory` which does this in the reverse order. + synchronize(); + } + atomicAdd(&aliveMemoryStatesCount, -1); break; } bool checkLeaks = Kotlin_memoryLeakCheckerEnabled() && destroyRuntime; @@ -2054,10 +2059,6 @@ void deinitMemory(MemoryState* memoryState, bool destroyRuntime) { cyclicDeinit(g_hasCyclicCollector); #endif // USE_CYCLIC_GC } - if (!destroyRuntime && memoryState->isMainThread) { - // If we are not destroying the runtime but we were the main thread, publish all changes to Kotlin globals. - synchronize(); - } // Actual GC only implemented in strict memory model at the moment. do { GC_LOG("Calling garbageCollect from DeinitMemory()\n") @@ -2077,7 +2078,7 @@ void deinitMemory(MemoryState* memoryState, bool destroyRuntime) { atomicAdd(&pendingDeinit, -1); #if TRACE_MEMORY - if (IsStrictMemoryModel && lastMemoryState && allocCount > 0) { + if (IsStrictMemoryModel && destroyRuntime && allocCount > 0) { MEMORY_LOG("*** Memory leaks, leaked %d containers ***\n", allocCount); dumpReachable("", memoryState->containers); } diff --git a/runtime/src/main/kotlin/kotlin/native/internal/Debugging.kt b/runtime/src/main/kotlin/kotlin/native/internal/Debugging.kt index b2273209649..638e0363df9 100644 --- a/runtime/src/main/kotlin/kotlin/native/internal/Debugging.kt +++ b/runtime/src/main/kotlin/kotlin/native/internal/Debugging.kt @@ -5,7 +5,7 @@ package kotlin.native.internal /* - * Internal utilities for debugging K/N compiler and runtime itself. + * Internal utilities for debugging K/N compiler and runtime. */ public object Debugging { public var forceCheckedShutdown: Boolean