Skip to content

Commit fd45f42

Browse files
committed
Merge pull request godotengine#101361 from Repiteo/core/isolate-ref-logic
Core: Isolate `Ref` forward declare logic
2 parents bdcbc98 + d9ef361 commit fd45f42

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

core/io/logger.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636
#include "core/templates/rb_set.h"
3737

3838
#include "modules/modules_enabled.gen.h" // For regex.
39+
3940
#ifdef MODULE_REGEX_ENABLED
4041
#include "modules/regex/regex.h"
41-
#else
42-
class RegEx : public RefCounted {};
4342
#endif // MODULE_REGEX_ENABLED
4443

4544
#if defined(MINGW_ENABLED) || defined(_MSC_VER)

core/object/ref_counted.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,15 @@ class Ref {
180180
// do a lot of referencing on references and stuff
181181
// mutexes will avoid more crashes?
182182

183-
if (reference && reference->unreference()) {
184-
memdelete(reference);
183+
if (reference) {
184+
// NOTE: `reinterpret_cast` is "safe" here because we know `T` has simple linear
185+
// inheritance to `RefCounted`. This guarantees that `T * == `RefCounted *`, which
186+
// allows us to declare `Ref<T>` with forward declared `T` types.
187+
if (reinterpret_cast<RefCounted *>(reference)->unreference()) {
188+
memdelete(reinterpret_cast<RefCounted *>(reference));
189+
}
190+
reference = nullptr;
185191
}
186-
reference = nullptr;
187192
}
188193

189194
template <typename... VarArgs>

0 commit comments

Comments
 (0)