Skip to content

Commit

Permalink
Report ownership check only once and add suggestion for GC languages
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Sep 21, 2024
1 parent 5a77be1 commit 29eb5b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 10 additions & 1 deletion util/godot/check_ref_ownership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace zylann::godot {

namespace {
bool g_enabled = true;
}
bool g_reported = false;
} // namespace

void CheckRefCountDoesNotChange::set_enabled(bool enabled) {
g_enabled = enabled;
Expand All @@ -14,4 +15,12 @@ bool CheckRefCountDoesNotChange::is_enabled() {
return g_enabled;
}

bool CheckRefCountDoesNotChange::was_reported() {
return g_reported;
}

void CheckRefCountDoesNotChange::mark_reported() {
g_reported = true;
}

} // namespace zylann::godot
9 changes: 7 additions & 2 deletions util/godot/check_ref_ownership.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ class CheckRefCountDoesNotChange {
return;
}
const int after_count = _rc->get_reference_count();
if (after_count != _initial_count) {
if (after_count != _initial_count && !was_reported()) {
mark_reported();
ZN_PRINT_ERROR(
format("Holding a reference to the passed {} outside {} is not allowed (count before: {}, "
"count after: {})",
"count after: {}). If you are using a garbage-collected language (like C#), "
"you may want to turn off this check in ProjectSettings.",
_rc->get_class(),
_method_name,
_initial_count,
Expand All @@ -44,6 +46,9 @@ class CheckRefCountDoesNotChange {
}

private:
static bool was_reported();
static void mark_reported();

const char *_method_name;
const RefCounted *_rc;
const int _initial_count;
Expand Down

0 comments on commit 29eb5b1

Please sign in to comment.