Skip to content

Commit 699237d

Browse files
committed
Merge pull request #102131 from dsnopek/classdb-bind-method-custom-leak
Fix memory leak when `ClassDB::bind_method_custom()` fails
2 parents 97c472e + e904c0c commit 699237d

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

core/object/class_db.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,26 +1866,30 @@ void ClassDB::_bind_compatibility(ClassInfo *type, MethodBind *p_method) {
18661866
void ClassDB::_bind_method_custom(const StringName &p_class, MethodBind *p_method, bool p_compatibility) {
18671867
OBJTYPE_WLOCK;
18681868

1869+
StringName method_name = p_method->get_name();
1870+
18691871
ClassInfo *type = classes.getptr(p_class);
18701872
if (!type) {
1871-
ERR_FAIL_MSG(vformat("Couldn't bind custom method '%s' for instance '%s'.", p_method->get_name(), p_class));
1873+
memdelete(p_method);
1874+
ERR_FAIL_MSG(vformat("Couldn't bind custom method '%s' for instance '%s'.", method_name, p_class));
18721875
}
18731876

18741877
if (p_compatibility) {
18751878
_bind_compatibility(type, p_method);
18761879
return;
18771880
}
18781881

1879-
if (type->method_map.has(p_method->get_name())) {
1882+
if (type->method_map.has(method_name)) {
18801883
// overloading not supported
1881-
ERR_FAIL_MSG(vformat("Method already bound '%s::%s'.", p_class, p_method->get_name()));
1884+
memdelete(p_method);
1885+
ERR_FAIL_MSG(vformat("Method already bound '%s::%s'.", p_class, method_name));
18821886
}
18831887

18841888
#ifdef DEBUG_METHODS_ENABLED
1885-
type->method_order.push_back(p_method->get_name());
1889+
type->method_order.push_back(method_name);
18861890
#endif
18871891

1888-
type->method_map[p_method->get_name()] = p_method;
1892+
type->method_map[method_name] = p_method;
18891893
}
18901894

18911895
MethodBind *ClassDB::_bind_vararg_method(MethodBind *p_bind, const StringName &p_name, const Vector<Variant> &p_default_args, bool p_compatibility) {

0 commit comments

Comments
 (0)