Hey there, I wonder if anyone here can help me. I ...
# announcements
m
Hey there, I wonder if anyone here can help me. I am doing some work with Kotlin Multiplatform and am running into this kind of exception:
kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
The context is quite convoluted and I'm not sure if it is relevant or not. I can elaborate further if anyone thinks it might help, but briefly: I've created a Kotlin Multiplatform library (see https://github.com/guardian/multiplatform-ophan) which can send various "events" to an analytics service called "Ophan". I've come up with a way to implement "Native Modules" for React Native applications using shared Kotlin Multiplatform code (see https://github.com/guardian/react-native-with-kotlin) Now I'm trying to put these two ideas together. But unfortunately, although the
multiplatform-ophan
library works as expected when consumed directly from Android or iOS, I run into the above exception on iOS when it is included in a React Native app via this extra layer of indirection. I don't expect anyone to help me debug this issue if it's anything to do with React Native and I appreciate you might think this seems like a very odd combination of things to be doing. But even just some general pointers about the kind of pitfalls you might expect in this area would be greatly appreciated.
One lead/idea is that it's something to do with
Charsets.UTF_8
, due to this stack trace:
Copy code
kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
        ... kfun:kotlinx.io.charsets.encodeToByteArrayImpl1$kotlinx-io@kotlinx.io.charsets.CharsetEncoder.(kotlin.CharSequence;kotlin.Int;kotlin.Int)kotlin.ByteArray + 443
       ...
and the fact that we are trying to do this at the point where it crashes:
val bytes = str.toByteArray(Charsets.UTF_8)
Could it be due to
Charsets.UTF_8
lacking the
@SharedImmutable
annotation?