3

I develop a K/N Multiplatform module and using the latest Kotlin 1.4.21. I found it often shows the following error message and terminate the app (crash) when I deinit this object:

Memory leaks detected, x objects leaked!
Use `Platform.isMemoryLeakCheckerActive = false` to avoid this check.

I cannot not figure out the reason of the memory leak, and following is my kotlin class using callback function:

class Detector(private val onEventDetected: ((event: Event) -> Unit)?) {

    fun processData(data: Data) {
        detectEvent(data)
    }

    private fun detectEvent(data: Data) {
        if (...) {
            ... // some logic
            onEventDetected?.invoke(event)
        }
    }
}

I found when I comment out the onEventDetected?.invoke(event), the error message got disappear. How can I implement the class to forbidden the memory leak issue?

Updated: Dec 21, 2020

I add WeakReference for different platforms:

// Common module
expect class WeakReference<T : Any>(referred: T) {
    fun clear()
    fun get(): T?
}
// For JVM
actual typealias WeakReference<T> = java.lang.ref.WeakReference<T>

// For iOS Native
actual typealias WeakReference<T> = kotlin.native.ref.WeakReference<T>

And update the class:

class Detector(private val onEventDetected: ((event: WeakReference<Event>) -> Unit)?) {

    fun processData(data: Data) {
        detectEvent(data)
    }

    private fun detectEvent(data: Data) {
        if (...) {
            ... // some logic
            onEventDetected?.invoke(WeakReference(event))
        }
    }
}

But the Memory leaks detected issues still happened. There is no clue to solve this problem :(

9
  • Are you using weak references dev.to/touchlab/swift-closures-in-kmp-12o7 ? Commented Dec 17, 2020 at 20:52
  • Not yet, how to using weak reference in my example? Cause my detector is in the common module and most of logic are in common. I find WeakReference exists in the iOS module only :( Commented Dec 18, 2020 at 8:53
  • @shadowsheep I update my code as the above, but the memory leaks detected issues still happened... Commented Dec 21, 2020 at 7:34
  • 1
    Hi James, could you post all your classes needed to debug this scenario, and a snippet that uses your class Detector. Having a minimal reproducible example should be helpful. The full error stacktrace should help too. Commented Dec 22, 2020 at 13:47
  • I create the project to reproduce this issue. You may download the project here: reurl.cc/e81VvK. Launch the app and click on green button to init the detector and click on the red button to kill the detector repeatedly, and you will see the crash log shortly about "Memory leaks detected, x objects leaked!" Commented Dec 24, 2020 at 2:17

1 Answer 1

1

Original findings:

Hi James, I've seen your sample project and I found out that the leak is due by the lambda when it's set from a thread different from the main thread and having as a parameter a class. If you remove parameters or if you use only primitive types like eventType raw and timestamp the leak doesn't occur. Actually I don't know why this happens, so you could try to file and issue on (youtrack.jetbrains.com) to see if this is a bug or a misuesed pattern. I'm courious about that too.

Update 2021-02-08

We have the official answer:

This is a known problem.

To workaround the problem, please make sure that when your program touches Kotlin code for the first time, it happens on the main thread. For example, declare an empty Kotlin function and call it from applicationDidLoad or whatever. Does it help?

Some of the effects of the problem are fixed in 1.4.30, but not all, so the recommendation above is still valid.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.