Fix potential crash when listing leaked objects

Note:
Casting to the C++ classes and calling the methods there would work as well,
but would require including he header files for the specific object types handled
here, which wouldn't be OK either.
This commit is contained in:
Pedro J. Estébanez 2020-05-06 14:19:04 +02:00
parent 3fb9c776ff
commit f09046a9ab
1 changed files with 11 additions and 4 deletions

View File

@ -2134,15 +2134,22 @@ void ObjectDB::cleanup() {
WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details)."); WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details).");
if (OS::get_singleton()->is_stdout_verbose()) { if (OS::get_singleton()->is_stdout_verbose()) {
// Ensure calling the native classes because if a leaked instance has a script
// that overrides any of those methods, it'd not be OK to call them at this point,
// now the scripting languages have already been terminated.
MethodBind *node_get_name = ClassDB::get_method("Node", "get_name");
MethodBind *resource_get_path = ClassDB::get_method("Resource", "get_path");
Variant::CallError call_error;
const ObjectID *K = NULL; const ObjectID *K = NULL;
while ((K = instances.next(K))) { while ((K = instances.next(K))) {
String node_name; String extra_info;
if (instances[*K]->is_class("Node")) if (instances[*K]->is_class("Node"))
node_name = " - Node name: " + String(instances[*K]->call("get_name")); extra_info = " - Node name: " + String(node_get_name->call(instances[*K], NULL, 0, call_error));
if (instances[*K]->is_class("Resource")) if (instances[*K]->is_class("Resource"))
node_name = " - Resource path: " + String(instances[*K]->call("get_path")); extra_info = " - Resource path: " + String(resource_get_path->call(instances[*K], NULL, 0, call_error));
print_line("Leaked instance: " + String(instances[*K]->get_class()) + ":" + itos(*K) + node_name); print_line("Leaked instance: " + String(instances[*K]->get_class()) + ":" + itos(*K) + extra_info);
} }
print_line("Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`)."); print_line("Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).");
} }