Skip to content

Commit

Permalink
feat(java): use Cleaner instead of finalize()
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaarf committed Sep 18, 2024
1 parent 64c272f commit 8ac24d3
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 17 deletions.
5 changes: 1 addition & 4 deletions dist/java/src/mp/code/BufferController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -116,10 +117,6 @@ public boolean stop() {
}

private static native void free(long self);
@Override
protected void finalize() {
free(this.ptr);
}

static {
NativeUtils.loadLibraryIfNeeded();
Expand Down
5 changes: 1 addition & 4 deletions dist/java/src/mp/code/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public final class Client {

Client(long ptr) {
this.ptr = ptr;
Extensions.CLEANER.register(this, () -> free(ptr));
}

/**
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions dist/java/src/mp/code/CursorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,10 +94,6 @@ public boolean stop() {
}

private static native void free(long self);
@Override
protected void finalize() {
free(this.ptr);
}

static {
NativeUtils.loadLibraryIfNeeded();
Expand Down
5 changes: 4 additions & 1 deletion dist/java/src/mp/code/Extensions.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package mp.code;

import java.io.IOException;
import java.lang.ref.Cleaner;

/**
* A class holding utility functions, as well as functions which are specific
* to this language's glue and don't necessarily have a counterpart in the
* 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
Expand Down
5 changes: 1 addition & 4 deletions dist/java/src/mp/code/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8ac24d3

Please sign in to comment.