1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-13 13:31:48 +00:00

Fix dangling and reassigned Variants

This commit addresses multiple issues with `Variant`s that point to an `Object`
which is later released, when it's tried to be accessed again.

Formerly, **while running on the debugger the system would check if the instance id was
still valid** to print warnings or return special values. Some cases weren't being
warned about whatsoever.

Also, a newly allocated `Object` could happen to be allocated at the same memory
address of an old one, making cases of use hard to find and having **`Variant`s pointing
to the old one magically reassigned to the new**.

This commit makes the engine realize all these situations **under debugging**
so you can detect and fix them. Running without a debugger attached will still
behave as it always did.

Also the warning messages have been extended and made clearer.

All that said, in the name of performance there's still one possible case of undefined
behavior: in multithreaded scripts there would be a race condition between a thread freeing
an `Object` and another one trying to operate on it. The latter may not realize the
`Object` has been freed soon enough. But that's a case of bad scripting that was never
supported anyway.
This commit is contained in:
Pedro J. Estébanez
2020-04-23 12:40:06 +02:00
parent 07c4dc1b30
commit d904d05e65
8 changed files with 378 additions and 166 deletions

View File

@@ -34,11 +34,16 @@
#include "core/hash_map.h"
#include "core/list.h"
#include "core/map.h"
#include "core/object_id.h"
#include "core/os/rw_lock.h"
#include "core/set.h"
#include "core/variant.h"
#include "core/vmap.h"
#ifdef DEBUG_ENABLED
#include <atomic> // For ObjectRC*
#endif
#define VARIANT_ARG_LIST const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant()
#define VARIANT_ARG_PASS p_arg1, p_arg2, p_arg3, p_arg4, p_arg5
#define VARIANT_ARG_DECLARE const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5
@@ -397,7 +402,7 @@ public: \
private:
class ScriptInstance;
typedef uint64_t ObjectID;
class ObjectRC;
class Object {
public:
@@ -477,6 +482,9 @@ private:
int _predelete_ok;
Set<Object *> change_receptors;
ObjectID _instance_id;
#ifdef DEBUG_ENABLED
std::atomic<ObjectRC *> _rc;
#endif
bool _predelete();
void _postinitialize();
bool _can_translate;
@@ -587,6 +595,10 @@ public:
return &ptr;
}
#ifdef DEBUG_ENABLED
ObjectRC *_use_rc();
#endif
bool _is_gpl_reversed() const { return false; }
_FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_id; }