Skip to content

Commit

Permalink
[GDScript] Fix lifetime of call base object
Browse files Browse the repository at this point in the history
Ensures that the lifetime of an `Object` outlasts calls to it

Also adds a check to `Object::~Object()` to check for locked state
  • Loading branch information
AThousandShips committed Aug 26, 2024
1 parent d48ec5d commit ee3f10d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,10 @@ void Object::detach_from_objectdb() {
}

Object::~Object() {
#ifdef DEBUG_ENABLED
CRASH_COND_MSG(_lock_index.get() != 1, "TODO message for object.");
#endif // DEBUG_ENABLED

if (script_instance) {
memdelete(script_instance);
}
Expand Down
16 changes: 16 additions & 0 deletions modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,27 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
gen->write_call_method_bind(result, base, method, arguments);
}
} else {
// Make temporary if member variable and ref-counted.
if (ClassDB::is_parent_class(class_name, "RefCounted") && base.mode == GDScriptCodeGenerator::Address::MEMBER) {
GDScriptCodeGenerator::Address temp = codegen.add_temporary(base.type);
gen->write_assign(temp, base);

base = temp;
}

gen->write_call(result, base, call->function_name, arguments);
}
} else if (base.type.has_type && base.type.kind == GDScriptDataType::BUILTIN) {
gen->write_call_builtin_type(result, base, base.type.builtin_type, call->function_name, arguments);
} else {
// Make temporary if member variable.
if (base.mode == GDScriptCodeGenerator::Address::MEMBER) {
GDScriptCodeGenerator::Address temp = codegen.add_temporary(base.type);
gen->write_assign(temp, base);

base = temp;
}

gen->write_call(result, base, call->function_name, arguments);
}
if (base.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# https://github.com/godotengine/godot/issues/86788

extends Node

class MyClassTyped:
func enter(outer):
outer.my_class_typed = MyClassTyped.new()
exit()

func exit():
pass

class MyClassUnTyped:
func enter(outer):
outer.my_class_untyped = MyClassUnTyped.new()
exit()

func exit():
pass

var my_class_typed := MyClassTyped.new()
var my_class_untyped = MyClassUnTyped.new()

func test():
my_class_typed.enter(self)
my_class_untyped.enter(self)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GDTEST_OK

0 comments on commit ee3f10d

Please sign in to comment.