diff --git a/dist/java/src/mp/code/BufferController.java b/dist/java/src/mp/code/BufferController.java index 220a339..0ae455f 100644 --- a/dist/java/src/mp/code/BufferController.java +++ b/dist/java/src/mp/code/BufferController.java @@ -17,6 +17,7 @@ public final class BufferController { BufferController(long ptr) { this.ptr = ptr; + Extensions.CLEANER.register(this, () -> free(ptr)); } private static native String get_name(long self); @@ -116,10 +117,6 @@ public final class BufferController { } private static native void free(long self); - @Override - protected void finalize() { - free(this.ptr); - } static { NativeUtils.loadLibraryIfNeeded(); diff --git a/dist/java/src/mp/code/Client.java b/dist/java/src/mp/code/Client.java index 1d20a92..12bd9c3 100644 --- a/dist/java/src/mp/code/Client.java +++ b/dist/java/src/mp/code/Client.java @@ -22,6 +22,7 @@ public final class Client { Client(long ptr) { this.ptr = ptr; + Extensions.CLEANER.register(this, () -> free(ptr)); } /** @@ -146,10 +147,6 @@ public final class Client { } private static native void free(long self); - @Override - protected void finalize() { - free(this.ptr); - } static { NativeUtils.loadLibraryIfNeeded(); diff --git a/dist/java/src/mp/code/CursorController.java b/dist/java/src/mp/code/CursorController.java index 7d2ce6a..ee3d238 100644 --- a/dist/java/src/mp/code/CursorController.java +++ b/dist/java/src/mp/code/CursorController.java @@ -16,6 +16,7 @@ public final class CursorController { CursorController(long ptr) { this.ptr = ptr; + Extensions.CLEANER.register(this, () -> free(ptr)); } private static native Cursor try_recv(long self) throws ControllerException; @@ -93,10 +94,6 @@ public final class CursorController { } private static native void free(long self); - @Override - protected void finalize() { - free(this.ptr); - } static { NativeUtils.loadLibraryIfNeeded(); diff --git a/dist/java/src/mp/code/Extensions.java b/dist/java/src/mp/code/Extensions.java index 6bd12a3..93e141a 100644 --- a/dist/java/src/mp/code/Extensions.java +++ b/dist/java/src/mp/code/Extensions.java @@ -1,6 +1,6 @@ package mp.code; -import java.io.IOException; +import java.lang.ref.Cleaner; /** * A class holding utility functions, as well as functions which are specific @@ -8,6 +8,9 @@ import java.io.IOException; * broader CodeMP API. */ public final class Extensions { + /** A {@link Cleaner} handling freeing of memory for library objects. */ + static final Cleaner CLEANER = Cleaner.create(); + /** * Hashes the given {@link String} using CodeMP's hashing algorithm (xxh3). * @param input the string to hash diff --git a/dist/java/src/mp/code/Workspace.java b/dist/java/src/mp/code/Workspace.java index c833030..3828995 100644 --- a/dist/java/src/mp/code/Workspace.java +++ b/dist/java/src/mp/code/Workspace.java @@ -22,6 +22,7 @@ public final class Workspace { Workspace(long ptr) { this.ptr = ptr; + Extensions.CLEANER.register(this, () -> free(ptr)); } private static native String get_workspace_id(long self); @@ -171,10 +172,6 @@ public final class Workspace { } private static native void free(long self); - @Override - protected void finalize() { - free(this.ptr); - } static { NativeUtils.loadLibraryIfNeeded();