Skip to content

Commit

Permalink
fix: call extism_function_free for functions created with extism_func…
Browse files Browse the repository at this point in the history
…tion_new
  • Loading branch information
zshipko committed Oct 26, 2023
1 parent 5a1d6ff commit be571f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ GEM

PLATFORMS
arm64-darwin-22
x86_64-linux

DEPENDENCIES
debug
Expand Down
11 changes: 10 additions & 1 deletion lib/extism/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ def initialize(wasm, environment: nil, functions: [], wasi: false, config: nil)
funcs_ptr.write_array_of_pointer(functions.map { |f| f.send(:pointer) })
@plugin = LibExtism.extism_plugin_new(code, wasm.bytesize, funcs_ptr, functions.length, wasi, errmsg)
if @plugin.null?
for f in functions
LibExtism.extism_function_free f.send(:pointer)
end
err = errmsg.read_pointer.read_string
LibExtism.extism_plugin_new_error_free errmsg.read_pointer
raise Error, err
end
$PLUGINS[object_id] = { plugin: @plugin }
$PLUGINS[object_id] = { plugin: @plugin, functions: functions }
ObjectSpace.define_finalizer(self, $FREE_PLUGIN)
return unless !config.nil? and @plugin.null?

Expand Down Expand Up @@ -96,6 +99,12 @@ def call(name, data, &block)
def free
return if @plugin.null?

functions = $PLUGINS[object_id][:functions]

for f in functions
LibExtism.extism_function_free f.send(:pointer)
end

$PLUGINS.delete(object_id)
LibExtism.extism_plugin_free(@plugin)
@plugin = nil
Expand Down

0 comments on commit be571f9

Please sign in to comment.