From 25a123d4dc5f3c810701dbd4aab66195620b7bfa Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Fri, 26 Apr 2013 10:41:55 +0200 Subject: [PATCH] stop leaking memory like there's no tomorrow --- interp.c | 4 ++++ list.c | 32 ++++++++++++++++++++++++-------- object.c | 15 ++++++++++++++- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/interp.c b/interp.c index b5d5b6d..3c2812b 100644 --- a/interp.c +++ b/interp.c @@ -193,6 +193,10 @@ struct Object* execute_branch(struct Object *ast) { *param4 = list_nth(stack, 5); destObject = method->value.c_method(destObject, param1, param2, param3, param4); + + // free the stack and the ast + free_object(ast); + free_object(stack); return destObject; } diff --git a/list.c b/list.c index ed1700e..68964c7 100644 --- a/list.c +++ b/list.c @@ -27,7 +27,7 @@ void mojo_list_remove(struct mojo_list* list) TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists); /* Also free the associated object : */ - free(e->obj); + free_object(e->obj); free(e); } @@ -60,8 +60,10 @@ int mojo_list_length(struct mojo_list *list) if (list == NULL) return -1; - struct mojo_list_elem *e; + struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head); int i = 0; + + TAILQ_FOREACH(e, &list->mojo_list_head, mojo_lists) { i++; } @@ -69,6 +71,20 @@ int mojo_list_length(struct mojo_list *list) return i; } + +void mojo_list_free(struct mojo_list *list) { + if (list == NULL) + return; + + struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head); + + TAILQ_FOREACH(e, &list->mojo_list_head, mojo_lists) { + TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists); + free_object(e->obj); + free(e); + } +} + /* Here begins the public facing list api. @@ -87,7 +103,7 @@ struct Object* create_list_object(void) list_object->super = nil_object; list_object->type = T_LIST; - list_object->name = "List"; + list_object->name = strdup("List"); list_object->value.l_value = mojo_list_init(); /* Create a second list object that contains the methods of the list object */ @@ -99,7 +115,7 @@ struct Object* create_list_object(void) l1->super = nil_object; l1->type = T_LIST; - l1->name = "List"; + l1->name = strdup("List"); l1->value.l_value = mojo_list_init(); l1->methods = nil_object; /* the methods of l1 are in l2, which is an empty list. This way, we don't fall into an infinite loop in clone_object() @@ -112,7 +128,7 @@ struct Object* create_list_object(void) fail("Unable to create a method %d %d", __FILE__, __LINE__); met->type = T_BUILTIN; - met->name = "+"; + met->name = strdup("+"); met->value.c_method = BUILTIN_CAST list_append; list_append(l1, met); @@ -122,7 +138,7 @@ struct Object* create_list_object(void) fail("Unable to create a method %d %d", __FILE__, __LINE__); met->type = T_BUILTIN; - met->name = "remove"; + met->name = strdup("remove"); met->value.c_method = BUILTIN_CAST list_remove; list_append(l1, met); @@ -132,7 +148,7 @@ struct Object* create_list_object(void) fail("Unable to create a method %d %d", __FILE__, __LINE__); met->type = T_BUILTIN; - met->name = "nth:"; + met->name = strdup("nth:"); met->value.c_method = BUILTIN_CAST list_nth; list_append(l1, met); @@ -142,7 +158,7 @@ struct Object* create_list_object(void) fail("Unable to create a method %d %d", __FILE__, __LINE__); met->type = T_BUILTIN; - met->name = "last"; + met->name = strdup("last"); met->value.c_method = BUILTIN_CAST list_last; list_append(l1, met); diff --git a/object.c b/object.c index adc6e5e..24416a6 100644 --- a/object.c +++ b/object.c @@ -58,7 +58,7 @@ struct Object* clone_object(struct Object *o) break; case T_LIST: - new->value.l_value = mojo_list_init(); + new->value.l_value = (struct mojo_list *) mojo_list_init(); for (i = 0; i < mojo_list_length(o->value.l_value); i++) { struct Object *t = clone_object((struct Object *) list_nth(o, i)); @@ -79,7 +79,20 @@ void free_object(struct Object *o) if (o == NULL) return; + /* + * FIXME: use our own lib for strings and don't use + * BSS strings which can't be freed. free(o->name); + */ + if (o->methods != NULL) + free_object(o->methods); + + if (o->symtab != NULL) + free_object(o->symtab); + + if (o->type == T_LIST && o->value.l_value != NULL) { + mojo_list_free(o->value.l_value); + } /* FIXME : free : the methods list, the methods and finally the object */ free(o); -- 2.43.0