From 8ac24d3078110e16ffca3e1d638511cff643d9f8 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 18 Sep 2024 15:36:11 +0200 Subject: [PATCH] feat(java): use Cleaner instead of finalize() --- dist/java/src/mp/code/BufferController.java | 5 +---- dist/java/src/mp/code/Client.java | 5 +---- dist/java/src/mp/code/CursorController.java | 5 +---- dist/java/src/mp/code/Extensions.java | 5 ++++- dist/java/src/mp/code/Workspace.java | 5 +---- 5 files changed, 8 insertions(+), 17 deletions(-) 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 boolean stop() { } 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 void refresh() throws ConnectionRemoteException { } 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 boolean stop() { } 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 @@ * 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 Event event() throws ControllerException { } private static native void free(long self); - @Override - protected void finalize() { - free(this.ptr); - } static { NativeUtils.loadLibraryIfNeeded();