Promote object validity checks to release builds

Extra:
- Optimized the debug-only check about why the object is null to determine if it's because it has been deleted (the RC is enough; no need to check the ObjectDB).
- Because of the previous point. the debugger being attached is not required anymore for giving the "Object was deleted" error; from now, it only matters that it's a debug build.
- `is_instance_valid()` is now trustworthy. It will return `true` if, and only if, the last object assigned to a `Variant` is still alive (and not if a new object happened to be created at the same memory address of the old one).
- Replacements of `instance_validate()` are used where possible `Variant::is_invalid_object()` is introduced to help with that. (GDScript's `is_instance_valid()` is good.)
This commit is contained in:
Pedro J. Estébanez
2021-09-21 10:30:34 +02:00
parent 1bbc7c9c3a
commit 26edc6cd41
13 changed files with 71 additions and 152 deletions

View File

@@ -961,7 +961,6 @@ void Object::cancel_delete() {
_predelete_ok = true;
}
#ifdef DEBUG_ENABLED
ObjectRC *Object::_use_rc() {
// The RC object is lazily created the first time it's requested;
// that way, there's no need to allocate and release it at all if this Object
@@ -989,7 +988,6 @@ ObjectRC *Object::_use_rc() {
rc = _rc.load(std::memory_order_acquire);
}
}
#endif
void Object::set_script_and_instance(const RefPtr &p_script, ScriptInstance *p_instance) {
//this function is not meant to be used in any of these ways
@@ -1927,9 +1925,7 @@ Object::Object() {
_emitting = false;
memset(_script_instance_bindings, 0, sizeof(void *) * MAX_SCRIPT_INSTANCE_BINDINGS);
script_instance = nullptr;
#ifdef DEBUG_ENABLED
_rc.store(nullptr, std::memory_order_release);
#endif
#ifdef TOOLS_ENABLED
_edited = false;
@@ -1942,14 +1938,12 @@ Object::Object() {
}
Object::~Object() {
#ifdef DEBUG_ENABLED
ObjectRC *rc = _rc.load(std::memory_order_acquire);
if (rc) {
if (rc->invalidate()) {
memdelete(rc);
}
}
#endif
if (script_instance) {
memdelete(script_instance);