From e5d203f452cd462724aa0bc6762c3aab6b243a44 Mon Sep 17 00:00:00 2001 From: Thaddeus Crews Date: Thu, 9 Jan 2025 10:02:27 -0600 Subject: [PATCH] Core: Isolate `Ref` forward declare logic --- core/io/logger.cpp | 3 +-- core/object/ref_counted.h | 11 ++++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/core/io/logger.cpp b/core/io/logger.cpp index f6a61282044..5da87cfe932 100644 --- a/core/io/logger.cpp +++ b/core/io/logger.cpp @@ -35,10 +35,9 @@ #include "core/os/time.h" #include "modules/modules_enabled.gen.h" // For regex. + #ifdef MODULE_REGEX_ENABLED #include "modules/regex/regex.h" -#else -class RegEx : public RefCounted {}; #endif // MODULE_REGEX_ENABLED #if defined(MINGW_ENABLED) || defined(_MSC_VER) diff --git a/core/object/ref_counted.h b/core/object/ref_counted.h index 927e457de2c..a90b7e06181 100644 --- a/core/object/ref_counted.h +++ b/core/object/ref_counted.h @@ -181,10 +181,15 @@ public: // do a lot of referencing on references and stuff // mutexes will avoid more crashes? - if (reference && reference->unreference()) { - memdelete(reference); + if (reference) { + // NOTE: `reinterpret_cast` is "safe" here, as the only way to achieve a non-null + // reference is after `ref_pointer` validates the type. This allows for passing + // forward-declared types without cascading dependency-chains. + if (reinterpret_cast(reference)->unreference()) { + memdelete(reinterpret_cast(reference)); + } + reference = nullptr; } - reference = nullptr; } template