Paul Woitaschek
01/08/2021, 5:30 PMIncorrectDereferenceException
It seems I cant use kotlin native on a background thread at all. (kotlin 1.4.21 and coroutines 1.4.2-native-mt)
import platform.darwin.DISPATCH_QUEUE_PRIORITY_DEFAULT
import platform.darwin.dispatch_async
import platform.darwin.dispatch_get_global_queue
import platform.posix.sleep
import kotlin.test.Test
class PersonGenerator
class MyTest {
@Test
fun test() {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT.toLong(), 0)) {
PersonGenerator()
}
sleep(1000)
}
}
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared MyTest.$test$lambda-0$FUNCTION_REFERENCE$43@8ae08b08 from other thread
Thomas
01/08/2021, 5:31 PMPaul Woitaschek
01/08/2021, 5:33 PMPaul Woitaschek
01/08/2021, 5:33 PMPaul Woitaschek
01/08/2021, 5:34 PMThomas
01/08/2021, 5:35 PMThomas
01/08/2021, 5:35 PMinitRuntimeIfNeeded()
in the lambdaPaul Woitaschek
01/08/2021, 5:36 PMPaul Woitaschek
01/08/2021, 5:36 PMPaul Woitaschek
01/08/2021, 5:37 PMThomas
01/08/2021, 5:38 PMThomas
01/08/2021, 5:39 PMPaul Woitaschek
01/08/2021, 5:39 PMThomas
01/08/2021, 5:40 PMThomas
01/08/2021, 5:41 PMThomas
01/08/2021, 5:44 PMIf you capture a reference to any object that is defined in the main thread outside ofinto the block insidewithContext
then it gets automatically frozen for transfer from the main thread to the background thread.withContext
basher
01/08/2021, 5:49 PMval work = { PersonGenerator() }.freeze()
might workbasher
01/08/2021, 5:50 PMPaul Woitaschek
01/08/2021, 6:27 PMPaul Woitaschek
01/08/2021, 6:27 PMPaul Woitaschek
01/09/2021, 8:02 AMThomas
01/09/2021, 10:05 AMPaul Woitaschek
01/09/2021, 10:05 AMThomas
01/09/2021, 10:06 AMThomas
01/09/2021, 10:07 AMimport platform.darwin.DISPATCH_QUEUE_PRIORITY_DEFAULT
import platform.darwin.dispatch_async
import platform.darwin.dispatch_get_global_queue
import platform.posix.sleep
import kotlin.test.Test
class PersonGenerator
class MyTest {
@Test
fun test() {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT.toLong(), 0), {
PersonGenerator()
}.freeze())
sleep(1000)
}
}
Paul Woitaschek
01/11/2021, 7:44 AM