try another approach
diff --git a/kotlin-native/runtime/src/mm/cpp/Memory.cpp b/kotlin-native/runtime/src/mm/cpp/Memory.cpp
index d4aa263..835cf29 100644
--- a/kotlin-native/runtime/src/mm/cpp/Memory.cpp
+++ b/kotlin-native/runtime/src/mm/cpp/Memory.cpp
@@ -634,5 +634,7 @@
}
void kotlin::OnMemoryAllocation(size_t totalAllocatedBytes) noexcept {
+ // Prevents unsafe class publication (see KT-58995).
+ std::atomic_thread_fence(std::memory_order_release);
mm::GlobalData::Instance().gcScheduler().setAllocatedBytes(totalAllocatedBytes);
}
diff --git a/kotlin-native/runtime/src/mm/cpp/ObjectOps.cpp b/kotlin-native/runtime/src/mm/cpp/ObjectOps.cpp
index e2d5ac5..7444497 100644
--- a/kotlin-native/runtime/src/mm/cpp/ObjectOps.cpp
+++ b/kotlin-native/runtime/src/mm/cpp/ObjectOps.cpp
@@ -75,8 +75,6 @@
AssertThreadState(threadData, ThreadState::kRunnable);
// TODO: Make this work with GCs that can stop thread at any point.
auto* object = threadData->gc().CreateObject(typeInfo);
- // Prevents unsafe class publication (see KT-58995).
- std::atomic_thread_fence(std::memory_order_release);
RETURN_OBJ(object);
}
@@ -84,8 +82,6 @@
AssertThreadState(threadData, ThreadState::kRunnable);
// TODO: Make this work with GCs that can stop thread at any point.
auto* array = threadData->gc().CreateArray(typeInfo, static_cast<uint32_t>(elements));
- // Prevents unsafe class publication (see KT-58995).
- std::atomic_thread_fence(std::memory_order_release);
// `ArrayHeader` and `ObjHeader` are expected to be compatible.
RETURN_OBJ(reinterpret_cast<ObjHeader*>(array));
}