Can I debug it further, I don't see where could a ...
# kotlin-native
a
Can I debug it further, I don't see where could a worker be leaked?
Copy code
Unfinished workers detected, 1 workers leaked!
Use `Platform.isMemoryLeakCheckerActive = false` to avoid this check.
Here's a small repro:
Copy code
object Testing {
    val event = MutableSharedFlow<Int>(extraBufferCapacity = 8)

    init {
        event.subscriptionCount
                // .onEach { println("Subscription Count: $it") }  // Stub, For seeing the behavior
                .launchIn(CoroutineScope(Dispatchers.Unconfined))
    }
}

fun main() = runBlocking {
    Testing
}
Edit: Sometimes the following error is generated as well:
Copy code
Memory leaks detected, 76 objects leaked!
Use `Platform.isMemoryLeakCheckerActive = false` to avoid this check.
Issue submitted: KT-43771 and KT-43772 both with minimal repro.
d
You're not cancelling the scope before exit.
a
Do I need to? By the way it doesn't happen if I use class and instantiate it instead using the singleton...
Which issue you're talking about ­čśů, I replied in context of the one with memory leak (not one with worker thread)
d
Yeah the leak. You've got to free the coroutine.
I'm talking about the code on slack, haven't looked at the tickets yet.
a
Ohh, thanks!
But unlike JVM I can't add a hook to run at the termination for cleanup, any suggestion for the cleanup?
In such a situation where I'm developing a lib and the consumer don't have access to the internals (those scopes), if I want to clear the memory before the exit...
d
Provide a close method. I think users should be somewhat aware of lifecycles.
a
@Dominaezzz Any thoughts on the worker leaks? That still happens if cancelled the CoroutineScope
d
Not sure tbh. Haven't really used coroutines on native.
n
Not a good idea to mix Workers and Coroutines together. Coroutines work fine with Kotlin Native, and don't require Workers to run.
a
@napperley sorry for late reply (apparantly I forgot to see the reply), but workers aren't involved anywhere, the leak is from internals of coroutines, see the repro I've shown: KT-43772