Skip to content

Commit 5b7d095

Browse files
authored
fix: add calls to extism_function_free to cleanup HostFunctions (#14)
It looks like there isn't a good way to define a destructor in Java, so this PR adds a `HostFunction.free` function that is automatically called when a Plugin is freed, if a `HostFunction` is never registered with a plugin then it should be called manually. Fixes #13
1 parent 1e1fd2c commit 5b7d095

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/main/java/org/extism/sdk/HostFunction.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class HostFunction<T extends HostUserData> {
1010

1111
private final LibExtism.InternalExtismFunction callback;
1212

13+
private boolean freed;
14+
1315
public final Pointer pointer;
1416

1517
public final String name;
@@ -21,7 +23,7 @@ public class HostFunction<T extends HostUserData> {
2123
public final Optional<T> userData;
2224

2325
public HostFunction(String name, LibExtism.ExtismValType[] params, LibExtism.ExtismValType[] returns, ExtismFunction f, Optional<T> userData) {
24-
26+
this.freed = false;
2527
this.name = name;
2628
this.params = params;
2729
this.returns = returns;
@@ -85,8 +87,15 @@ public void setNamespace(String name) {
8587
}
8688
}
8789

88-
HostFunction withNamespace(String name) {
90+
public HostFunction withNamespace(String name) {
8991
this.setNamespace(name);
9092
return this;
9193
}
94+
95+
public void free() {
96+
if (!this.freed){
97+
LibExtism.INSTANCE.extism_function_free(this.pointer);
98+
this.freed = true;
99+
}
100+
}
92101
}

src/main/java/org/extism/sdk/LibExtism.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Pointer extism_function_new(String name,
6262
Pointer userData,
6363
Pointer freeUserData);
6464

65+
void extism_function_free(Pointer function);
66+
6567
/**
6668
* Get the length of an allocated block
6769
* NOTE: this should only be called from host functions.

src/main/java/org/extism/sdk/Plugin.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Plugin implements AutoCloseable {
1717
*/
1818
private final Pointer pluginPointer;
1919

20+
private final HostFunction[] functions;
21+
2022
/**
2123
* @param manifestBytes The manifest for the plugin
2224
* @param functions The Host functions for th eplugin
@@ -40,13 +42,18 @@ public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions)
4042
withWASI,
4143
errormsg);
4244
if (p == null) {
45+
if (functions != null)
46+
for (int i = 0; i < functions.length; i++) {
47+
LibExtism.INSTANCE.extism_function_free(functions[i].pointer);
48+
}
4349
int errlen = LibExtism.INSTANCE.strlen(errormsg[0]);
4450
byte[] msg = new byte[errlen];
4551
errormsg[0].read(0, msg, 0, errlen);
4652
LibExtism.INSTANCE.extism_plugin_new_error_free(errormsg[0]);
4753
throw new ExtismException(new String(msg));
4854
}
4955

56+
this.functions = functions;
5057
this.pluginPointer = p;
5158
}
5259

@@ -114,6 +121,11 @@ protected String error() {
114121
* Frees a plugin from memory
115122
*/
116123
public void free() {
124+
if (this.functions != null){
125+
for (int i = 0; i < this.functions.length; i++) {
126+
this.functions[i].free();
127+
}
128+
}
117129
LibExtism.INSTANCE.extism_plugin_free(this.pluginPointer);
118130
}
119131

0 commit comments

Comments
 (0)