From f09046a9ab7de85f04127a2b461f2a809ea97fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Wed, 6 May 2020 14:19:04 +0200 Subject: [PATCH] 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. --- core/object.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/core/object.cpp b/core/object.cpp index 3acb8a34414..1d440a0404a 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -2134,15 +2134,22 @@ void ObjectDB::cleanup() { WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details)."); 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; while ((K = instances.next(K))) { - String node_name; + String extra_info; 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")) - node_name = " - Resource path: " + String(instances[*K]->call("get_path")); - print_line("Leaked instance: " + String(instances[*K]->get_class()) + ":" + itos(*K) + node_name); + 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) + 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()`)."); }