From dca653cc3abb8e8b2b4ed586055a08ed39c85d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Sun, 28 Jun 2020 01:27:03 +0200 Subject: [PATCH] Fix leaked ObjectRCs on object Variant reassignment Bonus: - Add some (un)likely magic - Use memdelete() instead of memfree() (not strictly needed, but more correct) --- core/object.cpp | 2 +- core/variant.cpp | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/core/object.cpp b/core/object.cpp index 1d440a0404a..b36f1420d2d 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -1996,7 +1996,7 @@ Object::~Object() { ObjectRC *rc = _rc.load(std::memory_order_acquire); if (rc) { if (rc->invalidate()) { - memfree(rc); + memdelete(rc); } } #endif diff --git a/core/variant.cpp b/core/variant.cpp index 94be8b6d43d..81386ce0867 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -1121,9 +1121,9 @@ void Variant::clear() { case OBJECT: { #ifdef DEBUG_ENABLED - if (_get_obj().rc) { - if (_get_obj().rc->decrement()) { - memfree(_get_obj().rc); + if (likely(_get_obj().rc)) { + if (unlikely(_get_obj().rc->decrement())) { + memdelete(_get_obj().rc); } } else { _get_obj().ref.unref(); @@ -2655,9 +2655,16 @@ void Variant::operator=(const Variant &p_variant) { } break; case OBJECT: { +#ifdef DEBUG_ENABLED + if (likely(_get_obj().rc)) { + if (unlikely(_get_obj().rc->decrement())) { + memdelete(_get_obj().rc); + } + } +#endif *reinterpret_cast(_data._mem) = p_variant._get_obj(); #ifdef DEBUG_ENABLED - if (_get_obj().rc) { + if (likely(_get_obj().rc)) { _get_obj().rc->increment(); } #endif