magnumrocha
04/16/2020, 4:19 PMstaticCFunction{..}
helper function from Kotlin/Native to wrap a Kotlin lambda function into a C function pointer. It only allows having unbound and non-capturing lambda functions. For example, it is not able to use a local variable from the function. We may only use globally visible declarations."
"globally visible declarations" -> how can access my global kotlin declarations inside the the lambda of staticCFunction
, because I have tested some approaches and only could have access to functions that use kotlin global functions (like println
), if I use some of my global scoped declarations my code fails on kotlin native call stack...Dominaezzz
04/16/2020, 8:14 PMuserPtr
?magnumrocha
04/17/2020, 4:58 PMConflatedBroadcastChannel
as a member, and every time I do:
userPtr.asStableRef<MyKClass>().get().broadcaster.offer(Unit)
I have EXC_BAD_ACCESS in my Swift Code on iOS side...magnumrocha
04/17/2020, 5:02 PMdispatch_async(dispatch_get_main_queue()) {...}
and the problem still happensDominaezzz
04/17/2020, 5:05 PMmagnumrocha
04/17/2020, 7:30 PMDominaezzz
04/17/2020, 8:08 PMmagnumrocha
04/17/2020, 8:10 PMclass MyKClass {
val broadcast = ConflatedBroadcastChannel<Unit>()
val selfPtr: StableRef<MyKClass>
init {
selfPtr = StableRef.create(this)
val callback: SomeCallbackClass = staticCFunction { value: SomeClassType, info: COpaquePointer? ->
if (info == null) { return@staticCFunction }
info.asStableRef<MyKClass>().get().broadcast.offer(Unit) // here causes EXC_BAD_ACCESS!
}
someNativeApi(selftPtr.asCPointer(), callback)
}
}
magnumrocha
04/17/2020, 8:15 PMDominaezzz
04/17/2020, 8:27 PMclass MyKClass {
val broadcast = ConflatedBroadcastChannel<Unit>()
init {
someNativeApiWrapper() { value: SomeClassType ->
broadcast.offer(Unit) // here causes EXC_BAD_ACCESS!
}
}
fun someNativeApiWrapper(callback: (value: SomeClassType) -> Unit) {
val callbackPtr = StableRef.create(callback)
someNativeApi(callbackPtr.asCPointer(), staticCFunction { value: SomeClassType, info: COpaquePointer? ->
if (info == null) { return@staticCFunction }
val realCallbackPtr = info.asStableRef<(SomeClassType) -> Unit>().get()
realCallbackPtr(value)
// Might free StableRef here
})
}
}