mirror of
https://github.com/hexedtech/codemp.git
synced 2024-12-23 05:14:54 +01:00
feat(java): use Cleaner instead of finalize()
This commit is contained in:
parent
64c272f0af
commit
8ac24d3078
5 changed files with 8 additions and 17 deletions
5
dist/java/src/mp/code/BufferController.java
vendored
5
dist/java/src/mp/code/BufferController.java
vendored
|
@ -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();
|
||||
|
|
5
dist/java/src/mp/code/Client.java
vendored
5
dist/java/src/mp/code/Client.java
vendored
|
@ -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();
|
||||
|
|
5
dist/java/src/mp/code/CursorController.java
vendored
5
dist/java/src/mp/code/CursorController.java
vendored
|
@ -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();
|
||||
|
|
5
dist/java/src/mp/code/Extensions.java
vendored
5
dist/java/src/mp/code/Extensions.java
vendored
|
@ -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
|
||||
|
|
5
dist/java/src/mp/code/Workspace.java
vendored
5
dist/java/src/mp/code/Workspace.java
vendored
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue