From 6f06cf0bf42628991eea3543a0e8e456fe91083e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 20 Mar 2020 19:58:57 +0100 Subject: [PATCH] src: delete BaseObjectWeakPtr data when pointee is gone Fix the condition for deleting the underlying data pointed to by a `BaseObjectWeakPtr`, which erroneously skipped that deletion when `ptr->get()` was `nullptr`. This fixes a memory leak reported by some of the tests. Refs: https://github.com/nodejs/node/pull/30374#issuecomment-601848973 PR-URL: https://github.com/nodejs/node/pull/32393 Reviewed-By: Matheus Marchini Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- src/base_object-inl.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/base_object-inl.h b/src/base_object-inl.h index c9b4e1491fc947..fc2611444c1af4 100644 --- a/src/base_object-inl.h +++ b/src/base_object-inl.h @@ -234,13 +234,13 @@ BaseObject* BaseObjectPtrImpl::get_base_object() const { template BaseObjectPtrImpl::~BaseObjectPtrImpl() { - if (get() == nullptr) return; if (kIsWeak) { - if (--pointer_data()->weak_ptr_count == 0 && + if (pointer_data() != nullptr && + --pointer_data()->weak_ptr_count == 0 && pointer_data()->self == nullptr) { delete pointer_data(); } - } else { + } else if (get() != nullptr) { get()->decrease_refcount(); } }