Skip to content

Commit 44a9852

Browse files
committed
fix: missing napi_delete_reference on ObjectWrap ref
1 parent 19002d5 commit 44a9852

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

napi-inl.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,13 +3341,7 @@ template <typename T>
33413341
inline Reference<T>::~Reference() {
33423342
if (_ref != nullptr) {
33433343
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
33493344
napi_delete_reference(_env, _ref);
3350-
#endif
33513345
}
33523346

33533347
_ref = nullptr;
@@ -4595,7 +4589,7 @@ template <typename T>
45954589
inline ObjectWrap<T>::~ObjectWrap() {
45964590
// If the JS object still exists at this point, remove the finalizer added
45974591
// through `napi_wrap()`.
4598-
if (!IsEmpty()) {
4592+
if (!IsEmpty() && !_finalized) {
45994593
Object object = Value();
46004594
// It is not valid to call `napi_remove_wrap()` with an empty `object`.
46014595
// This happens e.g. during garbage collection.
@@ -5044,8 +5038,10 @@ inline void ObjectWrap<T>::FinalizeCallback(node_addon_api_basic_env env,
50445038
(void)env;
50455039
T* instance = static_cast<T*>(data);
50465040

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;
50495045

50505046
// If class overrides the basic finalizer, execute it.
50515047
if constexpr (details::HasBasicFinalizer<T>::value) {

napi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,7 @@ class ObjectWrap : public InstanceWrap<T>, public Reference<Object> {
25142514
}
25152515

25162516
bool _construction_failed = true;
2517+
bool _finalized = false;
25172518
};
25182519

25192520
class HandleScope {

0 commit comments

Comments
 (0)