From: Elijah Cohen Date: Thu, 15 Aug 2024 20:08:26 +0000 (+0000) Subject: fixed memory leaks in dict X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=f77870fce954403f9ab63f44c80d8000013de888;p=klapaucius fixed memory leaks in dict --- diff --git a/src/Makefile b/src/Makefile index 325e039..4c65d96 100644 --- a/src/Makefile +++ b/src/Makefile @@ -16,7 +16,7 @@ TEST_OBJS:= $(filter-out $(BUILD)/repl.o,$(OBJS)) #LIBRARIES = poppler-glib MagickWand libexif taglib_c libmagic libavcodec libavformat sqlite3 uuid fuse3 libbsd-overlay -LIBRARIES = uuid libedit +LIBRARIES = libedit CFLAGS:= -g -Wall `pkg-config $(LIBRARIES) --cflags` LDFLAGS:= -g -Wall `pkg-config $(LIBRARIES) --libs` diff --git a/src/dict.c b/src/dict.c index c28498e..8c3176f 100644 --- a/src/dict.c +++ b/src/dict.c @@ -23,7 +23,7 @@ Sexpr* init_dict() { Sexpr* append_to_dict(Sexpr* dict, Sexpr* key, Sexpr* value) { // assumes dict well-formed, returns new dict // puts new things on the front of the dict, makes it so looking up gets the newest stuff first - Sexpr* new = cons(key, clone(value)); + Sexpr* new = cons(clone(key), clone(value)); // not yet sure if the above 'clone' is needed, or where the call should take place Sexpr* head = cons(new, car(dict)); dict->value.c->car = head; @@ -36,10 +36,13 @@ Sexpr* lookup(Sexpr* dict, Sexpr* key) { // returns nil if not found, returns (result) if it is Sexpr* node = car(dict); while(node->type != NIL) { - if(equal(key, car(car(node)))->type == T) { + Sexpr* eq = equal(key, car(car(node))); + if(eq->type == T) { Sexpr* value = clone(cdr(car(node))); + sexpr_free(eq); return cons(value, from_nil()); } + sexpr_free(eq); node = cdr(node); } return from_nil(); diff --git a/src/repl.c b/src/repl.c index 94420d8..d722715 100644 --- a/src/repl.c +++ b/src/repl.c @@ -2,7 +2,7 @@ #include #include -#include +//#include #include diff --git a/src/test.c b/src/test.c index d85f67d..6d5de14 100644 --- a/src/test.c +++ b/src/test.c @@ -78,11 +78,23 @@ void test_eq() { void test_dict() { printf("testing dict\n"); + printf("now with memory leak fixing\n"); Sexpr* d = init_dict(); - d = append_to_dict(d, from_sym("hello"), from_sym("world")); - d = append_to_dict(d, from_sym("asdf"), from_uint(544)); + Sexpr* k = from_sym("hello"); + Sexpr* v = from_sym("world"); + d = append_to_dict(d, k, v); + sexpr_free(k); + sexpr_free(v); + k = from_sym("asdf"); + v = from_uint(544); + d = append_to_dict(d, k, v); + sexpr_free(k); + sexpr_free(v); printf("lookup hello: %s\n", sprint_sexpr(lookup(d, from_sym("hello")))); printf("lookup asdf: %s\n", sprint_sexpr(lookup(d, from_sym("asdf")))); + sexpr_free(d); + + } void mem_parser() { @@ -140,20 +152,63 @@ void mem_testing() { 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); + + a = from_uint(545); + b = from_uint(545); + Sexpr* e = equal(a,b); + sexpr_free(a); + sexpr_free(b); + sexpr_free(e); + a = from_sym("hi"); + b = from_sym("hi"); + e = equal(a,b); + sexpr_free(a); + sexpr_free(b); + sexpr_free(e); +} + +void memtest_dict() { + printf("memtest dict\n"); + Sexpr* d = init_dict(); + + Sexpr* k = from_sym("hello"); + Sexpr* v = from_sym("world"); + d = append_to_dict(d, k, v); + sexpr_free(k); + sexpr_free(v); + k = from_sym("asdf"); + v = from_uint(454); + d = append_to_dict(d, k, v); + sexpr_free(k); + sexpr_free(v); + k = from_sym("hello"); + v = lookup(d, k); + char* out = sprint_sexpr(v); + printf("lookup hello: %s\n", out); + free(out); + sexpr_free(k); + sexpr_free(v); + k = from_sym("asdf"); + v = lookup(d, k); + out = sprint_sexpr(v); + printf("lookup hello: %s\n", out); + free(out); + sexpr_free(k); + sexpr_free(v); + + sexpr_free(d); } void run_tests(){ - mem_testing(); - mem_parser(); + //mem_testing(); + //mem_parser(); + memtest_dict(); //mem_hammer(); //test_basics(); //test2();