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