Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Complain if casting a freed object in a debug session #51095

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions core/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,25 @@ bool Variant::is_one() const {
return false;
}

ObjectID Variant::get_object_instance_id() const {
if (type != OBJECT) {
return 0;
}
#ifdef DEBUG_ENABLED
if (is_ref()) {
return !_get_obj().ref.is_null() ? _REF_OBJ_PTR(*this)->get_instance_id() : 0;
} else {
return _get_obj().rc->instance_id;
}
#else
if (is_ref() && _get_obj().ref.is_null()) {
return 0;
} else {
return _get_obj().obj->get_instance_id();
}
#endif
}

void Variant::reference(const Variant &p_variant) {
switch (type) {
case NIL:
Expand Down
2 changes: 2 additions & 0 deletions core/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class Variant {
bool is_zero() const;
bool is_one() const;

ObjectID get_object_instance_id() const;

operator bool() const;
operator signed int() const;
operator unsigned int() const; // this is the real one
Expand Down
12 changes: 12 additions & 0 deletions modules/gdscript/gdscript_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
*dst = Variant::construct(to_type, (const Variant **)&src, 1, err);

#ifdef DEBUG_ENABLED
if (src->get_type() == Variant::OBJECT && !src->is_ref() && ObjectDB::get_instance(src->get_object_instance_id()) == nullptr) {
err_text = "Trying to cast a deleted object.";
OPCODE_BREAK;
}
if (err.error != Variant::CallError::CALL_OK) {
err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
OPCODE_BREAK;
Expand All @@ -866,6 +870,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
GD_ERR_BREAK(!nc);

#ifdef DEBUG_ENABLED
if (src->get_type() == Variant::OBJECT && !src->is_ref() && ObjectDB::get_instance(src->get_object_instance_id()) == nullptr) {
err_text = "Trying to cast a deleted object.";
OPCODE_BREAK;
}
if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
err_text = "Invalid cast: can't convert a non-object value to an object type.";
OPCODE_BREAK;
Expand Down Expand Up @@ -894,6 +902,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
GD_ERR_BREAK(!base_type);

#ifdef DEBUG_ENABLED
if (src->get_type() == Variant::OBJECT && !src->is_ref() && ObjectDB::get_instance(src->get_object_instance_id()) == nullptr) {
err_text = "Trying to cast a deleted object.";
OPCODE_BREAK;
}
if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
OPCODE_BREAK;
Expand Down