msink / kotlin-libui

Kotlin/Native interop to libui: a portable GUI library
MIT License
648 stars 49 forks source link

Can't use MsgBox in worker #34

Open ObserverOfTime opened 3 years ago

ObserverOfTime commented 3 years ago

I'm trying to run a background task using a worker and display a message when an exception is caught.

Here's a minimal reproducible example:

package example

import kotlin.native.concurrent.TransferMode
import kotlin.native.concurrent.Worker
import libui.ktx.*

fun main() = appWindow("Test", 800, 600) {
    vbox {
        button("Test") {
            action {
                Worker.start().execute(TransferMode.UNSAFE, {}) {
                    MsgBox("This doesn't work")
                }
            }
        }
    }
}

That fails with this error when clicking the button:

Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
        at ThrowIncorrectDereferenceException (0x232c5c)
        at kfun:example.$<bridge-UNNN>main$lambda-2$<anonymous>_3_6$<anonymous>_5_8(kotlin.Unit)#internal (0x2485c0)
        at _ZN6Worker19processQueueElementEb (0x25d2ac)
        at _ZN12_GLOBAL__N_113workerRoutineEPv (0x25d96d)
        at  (0x7f70ac741422)
        at clone (0x7f70ac656bf3)
        at  ((nil))