mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-09 22:28:21 +00:00
fix(c-api) Fix memory leak in wasm_$name_vec_delete.
This patch does several things: 1. It applies our Rust patterns for C API by replacing the raw pointer by `Option<Box<T>>`, 2. It allows `wasm_$name_vec_delete` to handle null pointer, 3. Because it takes ownership of the `wasm_$name_vec_t`, the pointer is correctly dropped (which fix the memory leak).
This commit is contained in:
@@ -102,10 +102,15 @@ macro_rules! wasm_declare_vec {
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: *mut [<wasm_ $name _vec_t>]) {
|
||||
let vec = &mut *ptr;
|
||||
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: Option<Box<[<wasm_ $name _vec_t>]>>) {
|
||||
if ptr.is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut vec = ptr.unwrap();
|
||||
|
||||
if !vec.data.is_null() {
|
||||
Vec::from_raw_parts(vec.data, vec.size, vec.size);
|
||||
let _ = Vec::from_raw_parts(vec.data, vec.size, vec.size);
|
||||
vec.data = ::std::ptr::null_mut();
|
||||
vec.size = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user