12/01/2020, 3:23 AM
In a callback a kotlin.native.IncorrectDereferenceException is thrown after fetching a Stable Reference. Below is the stack trace:
Copy code
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared org.digieng.kmqtt.client.MqttClient@add4d8 from other thread
    at kfun:kotlin.Throwable#<init>(kotlin.String?){} (0x2a7e09)
    at kfun:kotlin.Exception#<init>(kotlin.String?){} (0x2a1a07)
    at kfun:kotlin.RuntimeException#<init>(kotlin.String?){} (0x2a1bb7)
    at kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} (0x2cb1c7)
    at ThrowIllegalObjectSharingException (0x2cd977)
    at  (0x3e4acb)
    at _ZNK16KRefSharedHolder3refIL11ErrorPolicy2EEEP9ObjHeaderv (0x3e48f3)
    at Kotlin_Interop_derefStablePointer (0x3e488e)
    at kfun:kotlinx.cinterop.StableRef#get(){}1:0 (0x2692e6)
    at kfun:org.digieng.kmqtt.client.onDeliveryComplete#internal (0x37522e)
    at _6f72672e64696769656e673a6b6d7174742d636c69656e74_knbridge46 (0x37602b)
    at MQTTClient_cycle (0x410d46)
    at MQTTClient_run (0x40c4dd)
    at  (0x7f7a508b56db)
    at clone (0x7f7a503c6a3f)
    at  ((nil))
Aborted (core dumped)
What is the way to resolve this issue? Note that the Linux platform is used as the target, which does allow for multi-threading. However the synchronous version of the Paho MQTT library is used which is single threaded.
Looks as though there isn't a way to share state with multi-threaded callbacks. There is nothing in the Kotlin Native Concurrency document that covers C callbacks ( ).