]> git.eli173.com Git - klapaucius/commitdiff
fixed memory leaks in parser
authorElijah Cohen <cohen@eli173.com>
Thu, 15 Aug 2024 06:35:22 +0000 (01:35 -0500)
committerElijah Cohen <cohen@eli173.com>
Thu, 15 Aug 2024 06:36:19 +0000 (01:36 -0500)
src/parser.c
src/sexpr.c
src/test.c

index 277796e0b565ad41a213d63a0e8be64fbe69bd3e..847c0895f78903bf2924e2a0d31c3bd69e4f23d0 100644 (file)
@@ -124,11 +124,16 @@ Sexpr* cons_parse(Sexpr* tokens) {
                if(cartype == SYM && strcmp(")", curr_car->value.s)==0) {
                        heads_stack = cons(curr_head, heads_stack);
                        curr_head = from_nil();
+                       sexpr_free(curr_car);
                }
                else if(cartype == SYM && strcmp("(", curr_car->value.s)==0) {
                        Sexpr* prev_head = car(heads_stack);
                        curr_head = cons(curr_head, prev_head);
+                       Sexpr* needtofreethistofixmemorybug = heads_stack;
                        heads_stack = cdr(heads_stack);
+                       free(needtofreethistofixmemorybug->value.c);
+                       free(needtofreethistofixmemorybug);
+                       sexpr_free(curr_car);
                }
                else {
                        curr_head = cons(curr_car, curr_head);
@@ -137,7 +142,6 @@ Sexpr* cons_parse(Sexpr* tokens) {
        }
        sexpr_free(reversedptr);
        sexpr_free(heads_stack);
-       sexpr_free(curr_car);
        return curr_head;
 }
 
@@ -147,8 +151,6 @@ Sexpr* parse(char* s) {
        Sexpr* tokens = tokenize(s);
        //printf("t: %s\n", sprint_sexpr(tokens));
        Sexpr* vals = vals_parse(tokens);
-       sexpr_free(vals);
-       vals = vals_parse(tokens);
        sexpr_free(tokens);
        //printf("v: %s\n", sprint_sexpr(vals));
        if(!balance_checker(vals)) {
index 69c37c57585684198bde9ebfa2e888d6347d34ea..8eb7d6a784cd795213996df071af03ccda16f56c 100644 (file)
@@ -25,8 +25,7 @@ Sexpr* from_t() {
 Sexpr* from_sym(char* s) {
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = SYM;
-       ret->value.s = malloc(strlen(s)+1);
-       strcpy(ret->value.s, s);
+       ret->value.s = strdup(s);
        return ret;
 }
 
@@ -111,6 +110,7 @@ void sexpr_free(Sexpr* s) {
        if(s->type == CONS) {
                sexpr_free(car(s));
                sexpr_free(cdr(s));
+               free(s->value.c);
        }
        if(s->type ==QUOTE) {
                sexpr_free(s->value.q);
@@ -131,7 +131,7 @@ Sexpr* clone(Sexpr* s) {
                ret = from_uint(s->value.u);
                break;
        case SYM:
-               ret = from_sym(strdup(s->value.s));
+               ret = from_sym(s->value.s);
                break;
        case BUILTIN:
                ret = from_opcode(s->value.b.opcode);
@@ -231,6 +231,7 @@ char* sprint_sexpr(Sexpr* s) {
                        currsize += strlen(carstr) + 1; // trailing space/close paren
                        out = realloc(out, currsize);
                        strcat(out, carstr);
+                       free(carstr);
                        strcat(out, " ");
                        curr_cell = cdr(curr_cell);
                }
@@ -246,6 +247,7 @@ char* sprint_sexpr(Sexpr* s) {
                        strcat(out, cdrstr);
                        strcat(out, ")");
                        out[currsize-1] = '\0';
+                       free(cdrstr);
                        return out;
                }
        }
index f77eca6ebec42149b5cf13d1ebc8cf5327da0728..d85f67d3a14afce66de67a78dafb98e71ebf5140 100644 (file)
@@ -87,15 +87,16 @@ void test_dict() {
 
 void mem_parser() {
        printf("starting parser memory testing\n");
-       char* toparse = "(car (cons 1 (cons 2 nil)))";
+       //char* toparse = "(car (cons 1 (cons 2 nil)))";
+       char* toparse = "(cons 1 2)";
        char* out;
        Sexpr* parsed;
        unsigned long l = 0;
-       while(l < 10000) {
+       while(l < 1) {
                parsed = parse(toparse);
                out = sprint_sexpr(parsed);
                sexpr_free(parsed);
-               //printf("%s\n", out);
+               printf("%s\n", out);
                free(out);
                //getchar();
                l++;
@@ -116,7 +117,42 @@ void mem_hammer() {
        }
 }
 
+
+void mem_testing() {
+       printf("mem testing\n");
+       Sexpr* a = cons(from_uint(5), cons(from_uint(6), from_nil()));
+       //Sexpr* a = cons(from_uint(1), from_sym("hi"));
+       char* out = sprint_sexpr(a);
+       printf("%s\n", out);
+       sexpr_free(a);
+       free(out);
+       a = tokenize("(cons 1 2)");
+       out = sprint_sexpr(a);
+       printf("%s\n", out);
+       free(out);
+       Sexpr* b = vals_parse(a);
+       sexpr_free(a);
+       out = sprint_sexpr(b);
+       printf("%s\n", out);
+       free(out);
+       Sexpr* r = reverse(b);
+       out = sprint_sexpr(r);
+       sexpr_free(r);
+       printf("%s\n", out);
+       free(out);
+
+
+       
+       Sexpr* c = cons_parse(b);
+       sexpr_free(b);
+       out = sprint_sexpr(c);
+       sexpr_free(c);
+       printf("%s\n", out);
+       free(out);
+}
+
 void run_tests(){
+       mem_testing();
        mem_parser();
        //mem_hammer();
        //test_basics();