Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KMP] Memory leak found in AnyAddress #4021

Open
10gic opened this issue Sep 12, 2024 · 6 comments
Open

[KMP] Memory leak found in AnyAddress #4021

10gic opened this issue Sep 12, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@10gic
Copy link
Contributor

10gic commented Sep 12, 2024

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.

@10gic 10gic added the bug Something isn't working label Sep 12, 2024
@trustwallet trustwallet deleted a comment from Ruyeduardo Sep 12, 2024
@satoshiotomakan
Copy link
Collaborator

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?

@10gic
Copy link
Contributor Author

10gic commented Sep 18, 2024

Hi @satoshiotomakan, I found that this is a common issue. It exists in HDWallet, PrivateKey, PublicKey, and others.

For swift binding (swift/Sources/Generated/AnyAddress.swift), we have deinit code to release memory:

    deinit {
        TWAnyAddressDelete(rawValue)
    }

For wasm binding (wasm/src/generated/AnyAddress.h), we have destructor to release memory:

    ~WasmAnyAddress() {
        TWAnyAddressDelete(instance);
    }

Kotlin don't support destructor, we need to think of other solutions to release memory.

@satoshiotomakan
Copy link
Collaborator

Hi @10gic, I see, thank you for the huge amount of work to find the root causes and propose the fix #4031, I'm checking it carefully right now.

@10gic
Copy link
Contributor Author

10gic commented Sep 18, 2024

Hi @satoshiotomakan, the pr #4031 only try to fix the issue #4030.

Issue #4030 and issue #4021 are two different types of memory leaks.

Issue #4021 hasn't been fixed yet.

@10gic
Copy link
Contributor Author

10gic commented Sep 18, 2024

Hi @satoshiotomakan, do you have any ideas on how to fix this issue (#4021)?

@satoshiotomakan
Copy link
Collaborator

@10gic
I scheduled a call this Friday with Kotlin engineers from another team. One of the ideas is to use native Swift, Java, JS wrappers to free the memory.
Unfortunately, I can't fully dedicate to the memory leaks as I'm currently focused on BTC PSBT, but will do the research and PR review end of this week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants