fix(c-api) Remove the memory leak in wasm_tabletype_element.

This commit is contained in:
Ivan Enderlin
2020-11-10 16:25:51 +01:00
parent 7212ab45fd
commit 9635e43c08

View File

@@ -12,6 +12,7 @@ const LIMITS_MAX_SENTINEL: u32 = u32::max_value();
pub(crate) struct WasmTableType {
pub(crate) table_type: TableType,
limits: Box<wasm_limits_t>,
content: Box<wasm_valtype_t>,
}
impl WasmTableType {
@@ -20,8 +21,13 @@ impl WasmTableType {
min: table_type.minimum as _,
max: table_type.maximum.unwrap_or(LIMITS_MAX_SENTINEL),
});
let content = Box::new(table_type.ty.into());
Self { table_type, limits }
Self {
table_type,
limits,
content,
}
}
}
@@ -77,15 +83,9 @@ pub unsafe extern "C" fn wasm_tabletype_limits(table_type: &wasm_tabletype_t) ->
table_type.inner().limits.as_ref()
}
// TODO: fix memory leak
// this function leaks memory because the returned limits pointer is not owned
#[no_mangle]
pub unsafe extern "C" fn wasm_tabletype_element(
table_type: &wasm_tabletype_t,
) -> *const wasm_valtype_t {
let table_type = table_type.inner().table_type;
Box::into_raw(Box::new(table_type.ty.into()))
pub unsafe extern "C" fn wasm_tabletype_element(table_type: &wasm_tabletype_t) -> &wasm_valtype_t {
table_type.inner().content.as_ref()
}
#[no_mangle]