From e904c0c014a09548f9058261cd09ddfb8fc2cc24 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Tue, 28 Jan 2025 10:53:43 -0600 Subject: [PATCH] Fix memory leak when `ClassDB::bind_method_custom()` fails --- core/object/class_db.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index ffd796de9cff..e6536fe8e98a 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -1866,9 +1866,12 @@ void ClassDB::_bind_compatibility(ClassInfo *type, MethodBind *p_method) { void ClassDB::_bind_method_custom(const StringName &p_class, MethodBind *p_method, bool p_compatibility) { OBJTYPE_WLOCK; + StringName method_name = p_method->get_name(); + ClassInfo *type = classes.getptr(p_class); if (!type) { - ERR_FAIL_MSG(vformat("Couldn't bind custom method '%s' for instance '%s'.", p_method->get_name(), p_class)); + memdelete(p_method); + ERR_FAIL_MSG(vformat("Couldn't bind custom method '%s' for instance '%s'.", method_name, p_class)); } if (p_compatibility) { @@ -1876,16 +1879,17 @@ void ClassDB::_bind_method_custom(const StringName &p_class, MethodBind *p_metho return; } - if (type->method_map.has(p_method->get_name())) { + if (type->method_map.has(method_name)) { // overloading not supported - ERR_FAIL_MSG(vformat("Method already bound '%s::%s'.", p_class, p_method->get_name())); + memdelete(p_method); + ERR_FAIL_MSG(vformat("Method already bound '%s::%s'.", p_class, method_name)); } #ifdef DEBUG_METHODS_ENABLED - type->method_order.push_back(p_method->get_name()); + type->method_order.push_back(method_name); #endif - type->method_map[p_method->get_name()] = p_method; + type->method_map[method_name] = p_method; } MethodBind *ClassDB::_bind_vararg_method(MethodBind *p_bind, const StringName &p_name, const Vector &p_default_args, bool p_compatibility) {