Fix memory leaks revealed by GC segments
diff --git a/src/lisp/Jmk2 b/src/lisp/Jmk2
index b9398ac..2fc98d4 100644
--- a/src/lisp/Jmk2
+++ b/src/lisp/Jmk2
@@ -36,7 +36,7 @@
rule valgrind [pwd]/lisp {
log VALGRIND "lisp test-gc.lisp"
- shell "LISP_LIBRARY_PATH=$::lisp_libpath valgrind --track-origins=yes --leak-check=full ./lisp test-gc.lisp"
+ shell "LISP_LIBRARY_PATH=$::lisp_libpath valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./lisp test-gc.lisp"
}
srcs main.c lisp.c compiler.c lib/std.c plat/linux.c istream.c gc.c \
diff --git a/src/lisp/compiler.dasc b/src/lisp/compiler.dasc
index e0e4eff..6549ecc 100644
--- a/src/lisp/compiler.dasc
+++ b/src/lisp/compiler.dasc
@@ -554,6 +554,9 @@
value_t (*f)() = link_program(&d);
+ dasm_free(&d);
+ free(args);
+
gc_prepare_call(0);
value_t val = f();
diff --git a/src/lisp/gc.c b/src/lisp/gc.c
index a6d88ac..826cd02 100644
--- a/src/lisp/gc.c
+++ b/src/lisp/gc.c
@@ -69,15 +69,6 @@
void *pointer = (void *)(value & ~HEAP_MASK);
struct alloc *alloc = pointer - sizeof(struct alloc);
- if (!pointer)
- {
- // TODO: Where are these coming from? Maybe this is a C
- // stack variable that we are interpreting as beign in
- // Lisp stack space on accident?
- fprintf(stderr, "lisp:gc:warning: value %x is a null pointer\n", value);
- return;
- }
-
// Only recursively mark if this hasn't been marked yet. i.e. prevent
// marking circular references twice
if (alloc->mark != gc_mark)
@@ -86,9 +77,6 @@
alloc->mark = gc_mark;
- // printf("[ GC ] val =");
- // printval(alloc_to_value(alloc), 2);
-
switch (alloc->type_tag)
{
case CONS_TAG: {
@@ -132,7 +120,6 @@
}
else
{
- fprintf(stderr, "[ GC ] freeing: %p\n", a);
// Free and remove from allocation list
struct alloc *p = a->prev, *n = a->next;
del_alloc(a);
@@ -207,17 +194,12 @@
value_t *args = seg->seg_end + 4;
for (int i = 0; i < seg->nargs; i++)
{
- fprintf(stderr, "Marking arg %d\n", i);
-
// mark arguments
_mark(args[i], &num_marked);
}
for (int i = 0; i < seg->nretained; i++)
{
- fprintf(stderr, "Marking retained %d\n", i);
- printval(seg->retained[i], 0);
-
_mark(seg->retained[i], &num_marked);
}
diff --git a/src/lisp/main.c b/src/lisp/main.c
index 2a37b4c..96a2f8e 100644
--- a/src/lisp/main.c
+++ b/src/lisp/main.c
@@ -39,4 +39,5 @@
free_all();
if (env)
del_env(env);
+ gc_pop_segment();
}