diff --git a/napi-inl.h b/napi-inl.h index 47e047087..2f2b71201 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3341,13 +3341,7 @@ template inline Reference::~Reference() { if (_ref != nullptr) { if (!_suppressDestruct) { -#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER - Env().PostFinalizer( - [](Napi::Env env, napi_ref ref) { napi_delete_reference(env, ref); }, - _ref); -#else napi_delete_reference(_env, _ref); -#endif } _ref = nullptr; @@ -4595,7 +4589,7 @@ template inline ObjectWrap::~ObjectWrap() { // If the JS object still exists at this point, remove the finalizer added // through `napi_wrap()`. - if (!IsEmpty()) { + if (!IsEmpty() && !_finalized) { Object object = Value(); // It is not valid to call `napi_remove_wrap()` with an empty `object`. // This happens e.g. during garbage collection. @@ -5044,8 +5038,9 @@ inline void ObjectWrap::FinalizeCallback(node_addon_api_basic_env env, (void)env; T* instance = static_cast(data); - // Prevent ~ObjectWrap from calling napi_remove_wrap - instance->_ref = nullptr; + // Prevent ~ObjectWrap from calling napi_remove_wrap. + // The instance->_ref should be deleted with napi_delete_reference in ~Reference. + instance->_finalized = true; // If class overrides the basic finalizer, execute it. if constexpr (details::HasBasicFinalizer::value) { diff --git a/napi.h b/napi.h index 7c103bf2c..99488f94e 100644 --- a/napi.h +++ b/napi.h @@ -2514,6 +2514,7 @@ class ObjectWrap : public InstanceWrap, public Reference { } bool _construction_failed = true; + bool _finalized = false; }; class HandleScope {