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:
committed by
Space Team
parent
6521729466
commit
1bf3d55bdf
@@ -0,0 +1,10 @@
|
||||
#import <Foundation/NSObject.h>
|
||||
|
||||
@interface DeallocFlagHolder : NSObject
|
||||
@property BOOL deallocated;
|
||||
@end
|
||||
|
||||
@interface ObjectWithDeallocFlag : NSObject
|
||||
@property (nonnull) DeallocFlagHolder* deallocFlagHolder;
|
||||
- (instancetype _Nonnull)sameObject;
|
||||
@end
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
import kotlin.test.*
|
||||
import kotlin.native.ref.WeakReference
|
||||
import kotlinx.cinterop.*
|
||||
import kotlinx.cinterop.internal.detachObjCObject
|
||||
import objcTests.*
|
||||
|
||||
class detachObjCObjectTests {
|
||||
@AfterTest
|
||||
private fun gc() {
|
||||
// An attempt to make sure the GC is fine after detachObjCObject.
|
||||
kotlin.native.runtime.GC.collect()
|
||||
kotlin.native.runtime.GC.collect()
|
||||
}
|
||||
|
||||
private fun checkThroughWeakRef(checkWeakRefBeforeReset: Boolean) {
|
||||
val obj = NSObject()
|
||||
val ref = WeakReference(obj)
|
||||
|
||||
if (checkWeakRefBeforeReset) {
|
||||
repeat(2) {
|
||||
val refValue = ref.value
|
||||
assertNotNull(refValue)
|
||||
// refValue is actually a new wrapper, so we need to reset it as well:
|
||||
detachObjCObject(refValue)
|
||||
// The next iteration will check that the object is not yet removed.
|
||||
}
|
||||
}
|
||||
|
||||
detachObjCObject(obj)
|
||||
val refValue = ref.value
|
||||
assertNull(refValue)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkThroughWeakRef() {
|
||||
checkThroughWeakRef(checkWeakRefBeforeReset = false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkThroughWeakRefWithMultipleWrappers() {
|
||||
checkThroughWeakRef(checkWeakRefBeforeReset = true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkThroughDeallocFlag() {
|
||||
val obj = ObjectWithDeallocFlag()
|
||||
val deallocFlagHolder = obj.deallocFlagHolder
|
||||
|
||||
assertFalse(deallocFlagHolder.deallocated)
|
||||
detachObjCObject(obj)
|
||||
assertTrue(deallocFlagHolder.deallocated)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkThroughDeallocFlagWithMultipleWrappers() {
|
||||
val obj = ObjectWithDeallocFlag()
|
||||
val deallocFlagHolder = obj.deallocFlagHolder
|
||||
|
||||
assertFalse(deallocFlagHolder.deallocated)
|
||||
|
||||
val sameObj = obj.sameObject() // Same object, different wrapper
|
||||
|
||||
detachObjCObject(obj)
|
||||
assertFalse(deallocFlagHolder.deallocated)
|
||||
|
||||
detachObjCObject(obj)
|
||||
assertFalse(deallocFlagHolder.deallocated)
|
||||
|
||||
detachObjCObject(sameObj)
|
||||
assertTrue(deallocFlagHolder.deallocated)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#import "detachObjCObject.h"
|
||||
|
||||
@implementation DeallocFlagHolder
|
||||
@end
|
||||
|
||||
@implementation ObjectWithDeallocFlag
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
self.deallocFlagHolder = [DeallocFlagHolder new];
|
||||
self.deallocFlagHolder.deallocated = NO;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
- (void)dealloc {
|
||||
self.deallocFlagHolder.deallocated = YES;
|
||||
}
|
||||
|
||||
- (instancetype _Nonnull)sameObject; {
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
Reference in New Issue
Block a user