From 3b39f794f9494c9cd4cd1276951d2cc6e2c3bb17 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 30 May 2026 19:51:29 +0100 Subject: [PATCH] verb: fix memory leaks --- bshell.runtime/include/bshell/verb.h | 2 + bshell.runtime/verb.c | 58 ++++++++++++++++------------ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/bshell.runtime/include/bshell/verb.h b/bshell.runtime/include/bshell/verb.h index 75fffdd..fadf894 100644 --- a/bshell.runtime/include/bshell/verb.h +++ b/bshell.runtime/include/bshell/verb.h @@ -124,6 +124,8 @@ FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_verb) FX_TYPE_CLASS_DECLARATION_END(bshell_verb) extern enum bshell_status bshell_init_all_verbs(void); +extern enum bshell_status bshell_cleanup_all_verbs(void); + extern fx_hashtable *bshell_get_all_verbs(void); extern bshell_verb *bshell_verb_get_by_id(enum bshell_verb_id id); extern bshell_verb *bshell_verb_get_by_name(const char *name); diff --git a/bshell.runtime/verb.c b/bshell.runtime/verb.c index 3135d39..44ad27c 100644 --- a/bshell.runtime/verb.c +++ b/bshell.runtime/verb.c @@ -25,9 +25,8 @@ bshell_verb *bshell_verb_create( return NULL; } - struct bshell_verb_p *verb_p = fx_object_get_private( - verb, - BSHELL_TYPE_VERB); + struct bshell_verb_p *verb_p + = fx_object_get_private(verb, BSHELL_TYPE_VERB); verb_p->v_id = id; verb_p->v_name = name; @@ -75,9 +74,8 @@ static fx_status get_name( { bshell_verb *verb = NULL; fx_value_get_object(verb_v, &verb); - struct bshell_verb_p *verb_p = fx_object_get_private( - verb, - BSHELL_TYPE_VERB); + struct bshell_verb_p *verb_p + = fx_object_get_private(verb, BSHELL_TYPE_VERB); *out = FX_CSTR(verb_p->v_name); return FX_SUCCESS; @@ -90,9 +88,8 @@ static fx_status get_alias_prefix( { bshell_verb *verb = NULL; fx_value_get_object(verb_v, &verb); - struct bshell_verb_p *verb_p = fx_object_get_private( - verb, - BSHELL_TYPE_VERB); + struct bshell_verb_p *verb_p + = fx_object_get_private(verb, BSHELL_TYPE_VERB); *out = FX_CSTR(verb_p->v_alias_prefix); return FX_SUCCESS; } @@ -104,9 +101,8 @@ static fx_status get_group( { bshell_verb *verb = NULL; fx_value_get_object(verb_v, &verb); - struct bshell_verb_p *verb_p = fx_object_get_private( - verb, - BSHELL_TYPE_VERB); + struct bshell_verb_p *verb_p + = fx_object_get_private(verb, BSHELL_TYPE_VERB); *out = FX_CSTR(verb_p->v_group); return FX_SUCCESS; } @@ -118,13 +114,17 @@ static fx_status get_description( { bshell_verb *verb = NULL; fx_value_get_object(verb_v, &verb); - struct bshell_verb_p *verb_p = fx_object_get_private( - verb, - BSHELL_TYPE_VERB); + struct bshell_verb_p *verb_p + = fx_object_get_private(verb, BSHELL_TYPE_VERB); *out = FX_CSTR(verb_p->v_description); return FX_SUCCESS; } +static void verb_fini(fx_object *obj, void *priv) +{ + struct bshell_verb_p *verb = priv; +} + FX_TYPE_CLASS_BEGIN(bshell_verb) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_INTERFACE_ENTRY(to_string) = NULL; @@ -143,6 +143,7 @@ FX_TYPE_DEFINITION_BEGIN(bshell_verb) FX_TYPE_NAME("bshell.verb"); FX_TYPE_CLASS(bshell_verb_class); FX_TYPE_INSTANCE_PRIVATE(struct bshell_verb_p); + FX_TYPE_INSTANCE_FINI(verb_fini); FX_TYPE_DEFINITION_END(bshell_verb) static fx_hashtable *all_verbs_by_name = NULL; @@ -405,18 +406,18 @@ enum bshell_status bshell_init_all_verbs(void) "dc", "Communications", "Breaks the link between a source and a destination"); - PUT_VERB( - READ, - "Read", - "rd", - "Communications", - "Acquires information from a source"); PUT_VERB( RECEIVE, "Receive", "rc", "Communications", "Accepts information sent from a source"); + PUT_VERB( + READ, + "Read", + "rd", + "Communications", + "Acquires information from a source"); PUT_VERB( SEND, "Send", @@ -788,6 +789,16 @@ enum bshell_status bshell_init_all_verbs(void) return BSHELL_SUCCESS; } +enum bshell_status bshell_cleanup_all_verbs(void) +{ + for (size_t i = 0; i < __BSHELL_VERB_MAX; i++) { + all_verbs_by_id[i] = NULL; + } + + fx_hashtable_unref(all_verbs_by_name); + return BSHELL_SUCCESS; +} + fx_hashtable *bshell_get_all_verbs(void) { return all_verbs_by_name; @@ -804,9 +815,8 @@ bshell_verb *bshell_verb_get_by_id(enum bshell_verb_id id) bshell_verb *bshell_verb_get_by_name(const char *name) { - const fx_value *val = fx_hashtable_get( - all_verbs_by_name, - &FX_CSTR(name)); + const fx_value *val + = fx_hashtable_get(all_verbs_by_name, &FX_CSTR(name)); if (!val) { return NULL; }