Skip to content

fix: add calls to extism_function_free to cleanup HostFunctions #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/main/java/org/extism/sdk/HostFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class HostFunction<T extends HostUserData> {

private final LibExtism.InternalExtismFunction callback;

private boolean freed;

public final Pointer pointer;

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

public HostFunction(String name, LibExtism.ExtismValType[] params, LibExtism.ExtismValType[] returns, ExtismFunction f, Optional<T> userData) {

this.freed = false;
this.name = name;
this.params = params;
this.returns = returns;
Expand Down Expand Up @@ -85,8 +87,15 @@ public void setNamespace(String name) {
}
}

HostFunction withNamespace(String name) {
public HostFunction withNamespace(String name) {
this.setNamespace(name);
return this;
}

public void free() {
if (!this.freed){
LibExtism.INSTANCE.extism_function_free(this.pointer);
this.freed = true;
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/extism/sdk/LibExtism.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Pointer extism_function_new(String name,
Pointer userData,
Pointer freeUserData);

void extism_function_free(Pointer function);

/**
* Get the length of an allocated block
* NOTE: this should only be called from host functions.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/extism/sdk/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Plugin implements AutoCloseable {
*/
private final Pointer pluginPointer;

private final HostFunction[] functions;

/**
* @param manifestBytes The manifest for the plugin
* @param functions The Host functions for th eplugin
Expand All @@ -40,13 +42,18 @@ public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions)
withWASI,
errormsg);
if (p == null) {
if (functions != null)
for (int i = 0; i < functions.length; i++) {
LibExtism.INSTANCE.extism_function_free(functions[i].pointer);
}
int errlen = LibExtism.INSTANCE.strlen(errormsg[0]);
byte[] msg = new byte[errlen];
errormsg[0].read(0, msg, 0, errlen);
LibExtism.INSTANCE.extism_plugin_new_error_free(errormsg[0]);
throw new ExtismException(new String(msg));
}

this.functions = functions;
this.pluginPointer = p;
}

Expand Down Expand Up @@ -114,6 +121,11 @@ protected String error() {
* Frees a plugin from memory
*/
public void free() {
if (this.functions != null){
for (int i = 0; i < this.functions.length; i++) {
this.functions[i].free();
}
}
LibExtism.INSTANCE.extism_plugin_free(this.pluginPointer);
}

Expand Down