diff --git a/Gemfile.lock b/Gemfile.lock index 5816bb6..af62640 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,6 +29,7 @@ GEM PLATFORMS arm64-darwin-22 + x86_64-linux DEPENDENCIES debug diff --git a/lib/extism/plugin.rb b/lib/extism/plugin.rb index 6158e1a..84f851c 100644 --- a/lib/extism/plugin.rb +++ b/lib/extism/plugin.rb @@ -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? @@ -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