Skip to content

Commit

Permalink
Fix Java JVM leak (#4092)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcoolish authored Nov 4, 2024
1 parent aeb534d commit 8121243
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 33 deletions.
28 changes: 3 additions & 25 deletions codegen/lib/templates/java/class.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,10 @@ public final class <%= entity.name %> {
throw new InvalidParameterException();
}

<%= entity.name %>PhantomReference.register(this, nativeHandle);
GenericPhantomReference.register(someObject, someHandle, handle -> {
nativeDelete(nativeHandle);
});
}

<%- end -%>
}
<% unless entity.methods.select{ |x| x.name == "Delete" }.empty? -%>
class <%= entity.name %>PhantomReference extends java.lang.ref.PhantomReference<<%= entity.name %>> {
private static java.util.Set<<%= entity.name %>PhantomReference> references = new HashSet<<%= entity.name %>PhantomReference>();
private static java.lang.ref.ReferenceQueue<<%= entity.name %>> queue = new java.lang.ref.ReferenceQueue<<%= entity.name %>>();
private long nativeHandle;

private <%= entity.name %>PhantomReference(<%= entity.name %> referent, long nativeHandle) {
super(referent, queue);
this.nativeHandle = nativeHandle;
}

static void register(<%= entity.name %> referent, long nativeHandle) {
references.add(new <%= entity.name %>PhantomReference(referent, nativeHandle));
}

public static void doDeletes() {
<%= entity.name %>PhantomReference ref = (<%= entity.name %>PhantomReference) queue.poll();
for (; ref != null; ref = (<%= entity.name %>PhantomReference) queue.poll()) {
<%= entity.name %>.nativeDelete(ref.nativeHandle);
references.remove(ref);
}
}
}
<% end -%>
4 changes: 4 additions & 0 deletions kotlin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ kotlin = "1.8.21"
agp = "8.0.0"
wire = "4.5.6"

androidx-test-runner = "1.5.2"

[libraries]
wire-runtime = { module = "com.squareup.wire:wire-runtime", version.ref = "wire" }
wire-compiler = { module = "com.squareup.wire:wire-compiler", version.ref = "wire" }

androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test-runner" }
17 changes: 10 additions & 7 deletions kotlin/wallet-core-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ kotlin {
}
}

getByName("commonTest") {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
Expand All @@ -85,6 +85,13 @@ kotlin {
implementation(npm(name = "webpack", version = "5.89.0"))
}
}

getByName("androidInstrumentedTest") {
dependsOn(commonTest)
dependencies {
implementation(libs.androidx.test.runner)
}
}
}

nativeTargets.forEach { nativeTarget ->
Expand Down Expand Up @@ -119,6 +126,8 @@ android {
arguments += listOf("-DCMAKE_BUILD_TYPE=Release", "-DKOTLIN=True", "-DTW_UNITY_BUILD=ON")
}
}

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
Expand All @@ -132,12 +141,6 @@ android {
viewBinding = false
}

androidComponents {
beforeVariants {
it.enable = it.name == "release"
}
}

externalNativeBuild {
cmake {
version = libs.versions.android.cmake.get()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.trustwallet.core

actual object LibLoader {
actual fun loadLibrary() {
System.loadLibrary("TrustWalletCore")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.trustwallet.core

actual object LibLoader {
actual fun loadLibrary() {
throw NotImplementedError()
System.loadLibrary("TrustWalletCore")
}
}

0 comments on commit 8121243

Please sign in to comment.