From f45f1aeb537fe8e73c1a76ca79653137910f22a1 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 19 Mar 2021 13:53:28 +0800 Subject: [PATCH] fix(napi): finalizer maybe_ref pointer value --- napi/src/js_values/object.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/napi/src/js_values/object.rs b/napi/src/js_values/object.rs index 75bd0ef0..431d8cbd 100644 --- a/napi/src/js_values/object.rs +++ b/napi/src/js_values/object.rs @@ -40,11 +40,12 @@ impl JsObject { F: FnOnce(FinalizeContext), { let mut maybe_ref = ptr::null_mut(); + let wrap_context = Box::leak(Box::new((native, finalize_cb, ptr::null_mut()))); check_status!(unsafe { sys::napi_add_finalizer( self.0.env, self.0.value, - Box::leak(Box::new((native, finalize_cb, maybe_ref))) as *mut _ as *mut c_void, + wrap_context as *mut _ as *mut c_void, Some( finalize_callback:: as unsafe extern "C" fn( @@ -56,7 +57,9 @@ impl JsObject { Box::leak(Box::new(finalize_hint)) as *mut _ as *mut c_void, &mut maybe_ref, // Note: this does not point to the boxed one… ) - }) + })?; + wrap_context.2 = maybe_ref; + Ok(()) } } @@ -75,7 +78,6 @@ unsafe extern "C" fn finalize_callback( let env = Env::from_raw(raw_env); callback(FinalizeContext { value, hint, env }); if !raw_ref.is_null() { - // … ⬆️ this branch is thus unreachable. let status = sys::napi_delete_reference(raw_env, raw_ref); debug_assert!( status == sys::Status::napi_ok,