Skip to content

Commit

Permalink
Complain if casting a freed object in a debug session
Browse files Browse the repository at this point in the history
The idea is to give the user a chance to realize a mistake that will cause a crash in a release build (or with no debugger attached).
  • Loading branch information
RandomShaper committed Sep 14, 2021
1 parent f2efa6f commit ddc7d7e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions modules/gdscript/gdscript_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,13 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a

GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);

#ifdef DEBUG_ENABLED
if (src->get_type() == Variant::OBJECT && !src->operator ObjectID().is_ref_counted() && ObjectDB::get_instance(src->operator ObjectID()) == nullptr) {
err_text = "Trying to cast a freed object.";
OPCODE_BREAK;
}
#endif

Callable::CallError err;
Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);

Expand All @@ -1255,6 +1262,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->operator ObjectID().is_ref_counted() && ObjectDB::get_instance(src->operator ObjectID()) == nullptr) {
err_text = "Trying to cast a freed 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 @@ -1283,6 +1294,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->operator ObjectID().is_ref_counted() && ObjectDB::get_instance(src->operator ObjectID()) == nullptr) {
err_text = "Trying to cast a freed 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

0 comments on commit ddc7d7e

Please sign in to comment.