]> khamidou — GitHub mirrors - mojo.git/commitdiff
stop leaking memory like there's no tomorrow
authorkarim hamidou <khamidou@gmail.com>
Fri, 26 Apr 2013 08:41:55 +0000 (10:41 +0200)
committerkarim hamidou <khamidou@gmail.com>
Fri, 26 Apr 2013 08:41:55 +0000 (10:41 +0200)
interp.c
list.c
object.c

index b5d5b6d6b0d14eda64045e8814b92734676388fd..3c2812bd538fecc6d64fb828df2967bf4b6a34b2 100644 (file)
--- 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 ed1700eab80db6fe7e122db596985cf1b25ec6c6..68964c7c9691973f6eafbc5fc29de3d5cceadbed 100644 (file)
--- 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);
 
index adc6e5e24141300a4f55ded12cd5655813291770..24416a6d2f276eee2bb05192d7708760d11f2602 100644 (file)
--- 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);