diff --git a/napi-inl.h b/napi-inl.h index ab920a4cf..65142cffa 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -2815,8 +2815,12 @@ inline ObjectWrap::ObjectWrap(const Napi::CallbackInfo& callbackInfo) { template inline ObjectWrap::~ObjectWrap() { + // If the JS object still exists at this point, remove the finalizer added + // through `napi_wrap()`. if (!IsEmpty()) { Object object = Value(); + // It is not valid to call `napi_remove_wrap()` with an empty `object`. + // This happens e.g. during garbage collection. if (!object.IsEmpty()) napi_remove_wrap(Env(), object, nullptr); }