From d2cf4548f6437cafebf025ef323133d445bd9d2e Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Mon, 15 Jan 2024 10:39:40 +0100 Subject: [PATCH] [K/N] Fix ExtraObjectData not being collected ^KT-63423 --- .../runtime/src/alloc/custom/cpp/AllocatorImpl.cpp | 10 +++++++++- kotlin-native/runtime/src/alloc/custom/cpp/GCApi.cpp | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/kotlin-native/runtime/src/alloc/custom/cpp/AllocatorImpl.cpp b/kotlin-native/runtime/src/alloc/custom/cpp/AllocatorImpl.cpp index d4f3af8ff41..4aa96a53437 100644 --- a/kotlin-native/runtime/src/alloc/custom/cpp/AllocatorImpl.cpp +++ b/kotlin-native/runtime/src/alloc/custom/cpp/AllocatorImpl.cpp @@ -78,5 +78,13 @@ size_t alloc::allocatedBytes() noexcept { void alloc::destroyExtraObjectData(mm::ExtraObjectData& extraObject) noexcept { extraObject.ReleaseAssociatedObject(); - extraObject.setFlag(mm::ExtraObjectData::FLAGS_FINALIZED); + if (extraObject.GetBaseObject()) { + // If there's an object attached to this extra object, the next + // GC sweep will have to resolve this cycle. + extraObject.setFlag(mm::ExtraObjectData::FLAGS_FINALIZED); + } else { + // If there's no object attached to this extra object, the next + // GC sweep will just collect this extra object. + extraObject.setFlag(mm::ExtraObjectData::FLAGS_SWEEPABLE); + } } diff --git a/kotlin-native/runtime/src/alloc/custom/cpp/GCApi.cpp b/kotlin-native/runtime/src/alloc/custom/cpp/GCApi.cpp index 95c1c30a6b7..25c9ec5d6e2 100644 --- a/kotlin-native/runtime/src/alloc/custom/cpp/GCApi.cpp +++ b/kotlin-native/runtime/src/alloc/custom/cpp/GCApi.cpp @@ -78,9 +78,11 @@ bool SweepObject(uint8_t* object, FinalizerQueue& finalizerQueue, gc::GCHandle:: bool SweepExtraObject(mm::ExtraObjectData* extraObject, gc::GCHandle::GCSweepExtraObjectsScope& gcHandle) noexcept { if (extraObject->getFlag(mm::ExtraObjectData::FLAGS_SWEEPABLE)) { + gcHandle.addSweptObject(); CustomAllocDebug("SweepExtraObject(%p): can be reclaimed", extraObject); return false; } + gcHandle.addKeptObject(sizeof(mm::ExtraObjectData)); CustomAllocDebug("SweepExtraObject(%p): is still needed", extraObject); return true; }