From d9ef361d122e480a81cd482224b560f973e06014 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 2965a1eac3..d6094fcc10 100644 --- a/core/io/logger.cpp +++ b/core/io/logger.cpp @@ -36,10 +36,9 @@ #include "core/templates/rb_set.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 77b6472dd4..7bb15d9eea 100644 --- a/core/object/ref_counted.h +++ b/core/object/ref_counted.h @@ -180,10 +180,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 because we know `T` has simple linear + // inheritance to `RefCounted`. This guarantees that `T * == `RefCounted *`, which + // allows us to declare `Ref` with forward declared `T` types. + if (reinterpret_cast(reference)->unreference()) { + memdelete(reinterpret_cast(reference)); + } + reference = nullptr; } - reference = nullptr; } template