@@ -3341,13 +3341,7 @@ template <typename T>
3341
3341
inline Reference<T>::~Reference () {
3342
3342
if (_ref != nullptr ) {
3343
3343
if (!_suppressDestruct) {
3344
- #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
3345
- Env ().PostFinalizer (
3346
- [](Napi::Env env, napi_ref ref) { napi_delete_reference (env, ref); },
3347
- _ref);
3348
- #else
3349
3344
napi_delete_reference (_env, _ref);
3350
- #endif
3351
3345
}
3352
3346
3353
3347
_ref = nullptr ;
@@ -4595,7 +4589,7 @@ template <typename T>
4595
4589
inline ObjectWrap<T>::~ObjectWrap () {
4596
4590
// If the JS object still exists at this point, remove the finalizer added
4597
4591
// through `napi_wrap()`.
4598
- if (!IsEmpty ()) {
4592
+ if (!IsEmpty () && !_finalized ) {
4599
4593
Object object = Value ();
4600
4594
// It is not valid to call `napi_remove_wrap()` with an empty `object`.
4601
4595
// This happens e.g. during garbage collection.
@@ -5044,8 +5038,10 @@ inline void ObjectWrap<T>::FinalizeCallback(node_addon_api_basic_env env,
5044
5038
(void )env;
5045
5039
T* instance = static_cast <T*>(data);
5046
5040
5047
- // Prevent ~ObjectWrap from calling napi_remove_wrap
5048
- instance->_ref = nullptr ;
5041
+ // Prevent ~ObjectWrap from calling napi_remove_wrap.
5042
+ // The instance->_ref should be deleted with napi_delete_reference in
5043
+ // ~Reference.
5044
+ instance->_finalized = true ;
5049
5045
5050
5046
// If class overrides the basic finalizer, execute it.
5051
5047
if constexpr (details::HasBasicFinalizer<T>::value) {
0 commit comments