Browse Source

Fix memory leak when `ClassDB::bind_method_custom()` fails

David Snopek 6 months ago
parent
commit
e904c0c014
1 changed files with 9 additions and 5 deletions
  1. 9 5
      core/object/class_db.cpp

+ 9 - 5
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) {
 void ClassDB::_bind_method_custom(const StringName &p_class, MethodBind *p_method, bool p_compatibility) {
 	OBJTYPE_WLOCK;
 	OBJTYPE_WLOCK;
 
 
+	StringName method_name = p_method->get_name();
+
 	ClassInfo *type = classes.getptr(p_class);
 	ClassInfo *type = classes.getptr(p_class);
 	if (!type) {
 	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) {
 	if (p_compatibility) {
@@ -1876,16 +1879,17 @@ void ClassDB::_bind_method_custom(const StringName &p_class, MethodBind *p_metho
 		return;
 		return;
 	}
 	}
 
 
-	if (type->method_map.has(p_method->get_name())) {
+	if (type->method_map.has(method_name)) {
 		// overloading not supported
 		// 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
 #ifdef DEBUG_METHODS_ENABLED
-	type->method_order.push_back(p_method->get_name());
+	type->method_order.push_back(method_name);
 #endif
 #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<Variant> &p_default_args, bool p_compatibility) {
 MethodBind *ClassDB::_bind_vararg_method(MethodBind *p_bind, const StringName &p_name, const Vector<Variant> &p_default_args, bool p_compatibility) {