feat(java): use Cleaner instead of finalize()

This commit is contained in:
zaaarf 2024-09-18 15:36:11 +02:00
parent 64c272f0af
commit 8ac24d3078
No known key found for this signature in database
GPG key ID: C91CFF9E2262BBA1
5 changed files with 8 additions and 17 deletions

View file

@ -17,6 +17,7 @@ public final class BufferController {
BufferController(long ptr) { BufferController(long ptr) {
this.ptr = ptr; this.ptr = ptr;
Extensions.CLEANER.register(this, () -> free(ptr));
} }
private static native String get_name(long self); private static native String get_name(long self);
@ -116,10 +117,6 @@ public final class BufferController {
} }
private static native void free(long self); private static native void free(long self);
@Override
protected void finalize() {
free(this.ptr);
}
static { static {
NativeUtils.loadLibraryIfNeeded(); NativeUtils.loadLibraryIfNeeded();

View file

@ -22,6 +22,7 @@ public final class Client {
Client(long ptr) { Client(long ptr) {
this.ptr = 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); private static native void free(long self);
@Override
protected void finalize() {
free(this.ptr);
}
static { static {
NativeUtils.loadLibraryIfNeeded(); NativeUtils.loadLibraryIfNeeded();

View file

@ -16,6 +16,7 @@ public final class CursorController {
CursorController(long ptr) { CursorController(long ptr) {
this.ptr = ptr; this.ptr = ptr;
Extensions.CLEANER.register(this, () -> free(ptr));
} }
private static native Cursor try_recv(long self) throws ControllerException; 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); private static native void free(long self);
@Override
protected void finalize() {
free(this.ptr);
}
static { static {
NativeUtils.loadLibraryIfNeeded(); NativeUtils.loadLibraryIfNeeded();

View file

@ -1,6 +1,6 @@
package mp.code; package mp.code;
import java.io.IOException; import java.lang.ref.Cleaner;
/** /**
* A class holding utility functions, as well as functions which are specific * A class holding utility functions, as well as functions which are specific
@ -8,6 +8,9 @@ import java.io.IOException;
* broader CodeMP API. * broader CodeMP API.
*/ */
public final class Extensions { 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). * Hashes the given {@link String} using CodeMP's hashing algorithm (xxh3).
* @param input the string to hash * @param input the string to hash

View file

@ -22,6 +22,7 @@ public final class Workspace {
Workspace(long ptr) { Workspace(long ptr) {
this.ptr = ptr; this.ptr = ptr;
Extensions.CLEANER.register(this, () -> free(ptr));
} }
private static native String get_workspace_id(long self); private static native String get_workspace_id(long self);
@ -171,10 +172,6 @@ public final class Workspace {
} }
private static native void free(long self); private static native void free(long self);
@Override
protected void finalize() {
free(this.ptr);
}
static { static {
NativeUtils.loadLibraryIfNeeded(); NativeUtils.loadLibraryIfNeeded();