trustwallet / wallet-core

Cross-platform, cross-blockchain wallet library.
https://developer.trustwallet.com/wallet-core
Apache License 2.0
2.77k stars 1.56k forks source link

[KMP] Memory leak found in AnyAddress #4021

Open 10gic opened 5 days ago

10gic commented 5 days ago

Describe the bug I found a memory leak in AnyAddress within the Kotlin package of Wallet Core.

dependencies {
    implementation "com.trustwallet:wallet-core-kotlin-android:4.1.7"
}

To Reproduce The following code can trigger a memory leak:

    println("Test started")
    val publicKeyHex = "044ba28b11af1561042b03b9d0f940446315af11358aa12d798050b3cf76265dab0f48b22ea1fc1f9560c969b966221f2821b746c4e56efaeaeec8689caf5843c9"
    var i = 1
    while (i <= 1_000_000) {
        val publicKey = PublicKey(publicKeyHex.hexToByteArray(), PublicKeyType.SECP256k1Extended)
        val anyAddress = AnyAddress(publicKey, Ethereum, Derivation.Default)   // This leaks memory!
        if (i % 10_000 == 0) {
            println("Test case $i run")
        }
        i++
    }
    println("Test completed")

Expected behavior No memory leak.

Screenshots Memory Profiler Screenshot:

image

Additional context The above test was conducted on Android. Based on the generated code in directory wallet-core-kotlin, this issue should also exist on iOS.

satoshiotomakan commented 5 days ago

Hi @10gic, good catch! I'll investigate what can be the reason for the memory leak. Could you please advise if you caught the memory leak while using other WC bindings? For example, PrivateKey or HDWallet?