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:
@ -44,13 +44,14 @@
|
||||
#include "core/math/transform_2d.h"
|
||||
#include "core/math/vector3.h"
|
||||
#include "core/node_path.h"
|
||||
#include "core/object_id.h"
|
||||
#include "core/pool_vector.h"
|
||||
#include "core/ref_ptr.h"
|
||||
#include "core/rid.h"
|
||||
#include "core/ustring.h"
|
||||
|
||||
class RefPtr;
|
||||
class Object;
|
||||
class ObjectRC;
|
||||
class Node; // helper
|
||||
class Control; // helper
|
||||
|
||||
@ -72,6 +73,13 @@ typedef PoolVector<Color> PoolColorArray;
|
||||
#define GCC_ALIGNED_8
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
// Ideally, an inline member of ObjectRC, but would cause circular includes
|
||||
#define _OBJ_PTR(m_variant) ((m_variant)._get_obj().rc ? (m_variant)._get_obj().rc->get_ptr() : reinterpret_cast<Ref<Reference> *>((m_variant)._get_obj().ref.get_data())->ptr())
|
||||
#else
|
||||
#define _OBJ_PTR(m_variant) ((m_variant)._get_obj().obj)
|
||||
#endif
|
||||
|
||||
class Variant {
|
||||
public:
|
||||
// If this changes the table in variant_op must be updated
|
||||
@ -127,7 +135,21 @@ private:
|
||||
|
||||
struct ObjData {
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
// Will be null for every type deriving from Reference as they have their
|
||||
// own reference count mechanism
|
||||
ObjectRC *rc;
|
||||
// This is for allowing debug build to check for instance ID validity,
|
||||
// so warnings are shown in debug builds when a stray Variant (one pointing
|
||||
// to a released Object) would have happened.
|
||||
// If it's zero, that means the Variant is has a legit null object value,
|
||||
// thus not needing instance id validation.
|
||||
ObjectID instance_id;
|
||||
#else
|
||||
Object *obj;
|
||||
#endif
|
||||
// Always initialized, but will be null if the Ref<> assigned was null
|
||||
// or this Variant is not even holding a Reference-derived object
|
||||
RefPtr ref;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user