Skip to content

Commit

Permalink
Merge pull request godotengine#102131 from dsnopek/classdb-bind-metho…
Browse files Browse the repository at this point in the history
…d-custom-leak

Fix memory leak when `ClassDB::bind_method_custom()` fails
  • Loading branch information
Repiteo committed Jan 30, 2025
2 parents 97c472e + e904c0c commit 699237d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions core/object/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1866,26 +1866,30 @@ 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) {
_bind_compatibility(type, p_method);
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<Variant> &p_default_args, bool p_compatibility) {
Expand Down

0 comments on commit 699237d

Please sign in to comment.