Skip to content

Commit 98aae33

Browse files
authoredNov 7, 2024··
fix: missing napi_delete_reference on ObjectWrap ref (#1607)
1 parent c76b757 commit 98aae33

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed
 

‎napi-inl.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -3341,6 +3341,8 @@ template <typename T>
33413341
inline Reference<T>::~Reference() {
33423342
if (_ref != nullptr) {
33433343
if (!_suppressDestruct) {
3344+
// TODO(legendecas): napi_delete_reference should be invoked immediately.
3345+
// Fix this when https://github.com/nodejs/node/pull/55620 lands.
33443346
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
33453347
Env().PostFinalizer(
33463348
[](Napi::Env env, napi_ref ref) { napi_delete_reference(env, ref); },
@@ -4595,7 +4597,7 @@ template <typename T>
45954597
inline ObjectWrap<T>::~ObjectWrap() {
45964598
// If the JS object still exists at this point, remove the finalizer added
45974599
// through `napi_wrap()`.
4598-
if (!IsEmpty()) {
4600+
if (!IsEmpty() && !_finalized) {
45994601
Object object = Value();
46004602
// It is not valid to call `napi_remove_wrap()` with an empty `object`.
46014603
// This happens e.g. during garbage collection.
@@ -5044,8 +5046,10 @@ inline void ObjectWrap<T>::FinalizeCallback(node_addon_api_basic_env env,
50445046
(void)env;
50455047
T* instance = static_cast<T*>(data);
50465048

5047-
// Prevent ~ObjectWrap from calling napi_remove_wrap
5048-
instance->_ref = nullptr;
5049+
// Prevent ~ObjectWrap from calling napi_remove_wrap.
5050+
// The instance->_ref should be deleted with napi_delete_reference in
5051+
// ~Reference.
5052+
instance->_finalized = true;
50495053

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

‎napi.h

+1
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)
Please sign in to comment.