-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[native-mt] Fix ObjC autorelease object leaks with native-mt dispatch…
- Loading branch information
Showing
6 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import kotlinx.cinterop.* | ||
|
||
internal actual inline fun platformAutoreleasePool(crossinline block: () -> Unit): Unit = autoreleasepool { block() } |
40 changes: 40 additions & 0 deletions
40
kotlinx-coroutines-core/nativeDarwin/test/AutoreleaseLeakTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import kotlin.native.ref.* | ||
import kotlin.native.concurrent.* | ||
import kotlin.native.internal.* | ||
import platform.Foundation.* | ||
import platform.darwin.NSObject | ||
import kotlinx.cinterop.* | ||
import kotlin.test.* | ||
|
||
class AutoreleaseLeakTest : TestBase() { | ||
private val testThread = currentThread() | ||
|
||
@Test | ||
fun testObjCAutorelease() { | ||
val weakRef = AtomicReference<WeakReference<NSObject>?>(null) | ||
|
||
runTest { | ||
withContext(Dispatchers.Default) { | ||
val job = launch { | ||
assertNotEquals(testThread, currentThread()) | ||
val objcObj = NSObject() | ||
weakRef.value = WeakReference(objcObj).freeze() | ||
|
||
// Emulate an autorelease return value in native code. | ||
objc_retainAutoreleaseReturnValue(objcObj.objcPtr()) | ||
} | ||
job.join() | ||
GC.collect() | ||
} | ||
|
||
assertNotNull(weakRef.value) | ||
assertNull(weakRef.value?.value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
internal actual inline fun platformAutoreleasePool(crossinline block: () -> Unit): Unit = block() |