Native: add detachObjCObject internal function for Obj-C objects

For a Kotlin wrapper of an Objective-C object, this functions zeroes
and releases the strong reference from the Kotlin wrapper to the
Objective-C object.
Usually, such a release happens only when the Kotlin wrapper is GCed.
So using this function can help that happen sooner.

^KT-59134
This commit is contained in:
Svyatoslav Scherbina
2023-06-01 15:42:31 +02:00
committed by Space Team
parent 6521729466
commit 1bf3d55bdf
6 changed files with 154 additions and 1 deletions
@@ -366,6 +366,19 @@ void Kotlin_objc_release(id ptr) {
objc_release(ptr);
}
void Kotlin_objc_detachObjCObject(KRef ref) {
id associatedObject = GetAssociatedObject(ref);
while (true) {
if (associatedObject == nullptr) break;
id actualAssociatedObject = AtomicCompareAndSwapAssociatedObject(ref, associatedObject, nullptr);
if (actualAssociatedObject == associatedObject) {
Kotlin_ObjCExport_releaseAssociatedObject(associatedObject);
break;
}
associatedObject = actualAssociatedObject;
}
}
} // extern "C"
#else // KONAN_OBJC_INTEROP
@@ -397,6 +410,10 @@ void Kotlin_objc_release(void* ptr) {
RuntimeAssert(false, "Objective-C interop is disabled");
}
void Kotlin_objc_detachObjCObject(void* ref) {
RuntimeAssert(false, "Objective-C interop is disabled");
}
} // extern "C"
#endif // KONAN_OBJC_INTEROP