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:
Ivan Enderlin
2020-12-01 14:25:59 +01:00
parent 8899509996
commit 527b7061cc

View File

@@ -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;
}