diff --git a/src/config.rs b/src/config.rs index 57c8f44..9e20e5e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -34,5 +34,9 @@ pub fn get_memory(key: impl AsRef) -> Result, Error> { /// let my_config = config::get("my_config")?.unwrap_or(0u32); /// ``` pub fn get(key: impl AsRef) -> Result, Error> { - Ok(get_memory(key)?.map(|x| x.to_string().expect("Config value is not a valid string"))) + Ok(get_memory(key)?.map(|x| { + let s = x.to_string().expect("Config value is not a valid string"); + x.free(); + s + })) } diff --git a/src/http.rs b/src/http.rs index c523e58..e3d2d4f 100644 --- a/src/http.rs +++ b/src/http.rs @@ -44,6 +44,8 @@ pub fn request( }; let data = body.as_ref().map(|x| x.offset()).unwrap_or(0); let offs = unsafe { extism::http_request(req.offset(), data) }; + req.free(); + body.map(|x| x.free()); let status = unsafe { extism::http_status_code() }; let len = unsafe { extism::length_unsafe(offs) }; Ok(HttpResponse { diff --git a/src/macros.rs b/src/macros.rs index a0e8acc..1d25149 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -3,7 +3,8 @@ macro_rules! log { ($lvl:expr, $($arg:tt)+) => {{ let fmt = format!($($arg)+); let memory = $crate::Memory::from_bytes(&fmt).unwrap(); - memory.log($lvl) + memory.log($lvl); + memory.free() }} } diff --git a/src/var.rs b/src/var.rs index 71587ec..74a3c9a 100644 --- a/src/var.rs +++ b/src/var.rs @@ -32,7 +32,11 @@ pub fn get_memory(key: impl AsRef) -> Result, Error> { /// let my_var = var::get("my_var")?.unwrap_or(0u32); /// ``` pub fn get(key: impl AsRef) -> Result, Error> { - match get_memory(key)?.map(|x| x.to_vec()) { + match get_memory(key)?.map(|x| { + let res = x.to_vec(); + x.free(); + res + }) { Some(v) => Ok(Some(T::from_bytes(&v)?)), None => Ok(None), } @@ -56,6 +60,8 @@ pub fn set(key: impl AsRef, val: impl ToMemory) -> Result<(), Error> { let val = val.to_memory()?; let key = Memory::from_bytes(key.as_ref().as_bytes())?; unsafe { extism::var_set(key.offset(), val.offset()) } + key.free(); + val.free(); Ok(()) } @@ -74,5 +80,6 @@ pub fn set(key: impl AsRef, val: impl ToMemory) -> Result<(), Error> { pub fn remove(key: impl AsRef) -> Result<(), Error> { let key = Memory::from_bytes(key.as_ref().as_bytes())?; unsafe { extism::var_set(key.offset(), 0) }; + key.free(); Ok(()) } diff --git a/test/code.wasm b/test/code.wasm index 75e1a83..23b54ad 100755 Binary files a/test/code.wasm and b/test/code.wasm differ diff --git a/test/host_function.wasm b/test/host_function.wasm index bba3366..8027095 100755 Binary files a/test/host_function.wasm and b/test/host_function.wasm differ diff --git a/test/http.wasm b/test/http.wasm index b082736..dad0855 100755 Binary files a/test/http.wasm and b/test/http.wasm differ