From 3e248bce8cfe68e495829e27222140a17125e7f0 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Thu, 18 Apr 2024 21:01:28 +0100 Subject: [PATCH] Plug several memory leaks --- src/jit/jit-exits.c | 2 +- src/lower.c | 8 +++++++- src/rt/model.c | 6 ++++++ src/vcode.c | 7 +++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/jit/jit-exits.c b/src/jit/jit-exits.c index 9705d239..e6124a25 100644 --- a/src/jit/jit-exits.c +++ b/src/jit/jit-exits.c @@ -348,7 +348,7 @@ void x_instance_name(attr_kind_t kind, text_buf_t *tb) { assert(kind == ATTR_INSTANCE_NAME || kind == ATTR_PATH_NAME); - jit_stack_trace_t *trace = jit_stack_trace(); + jit_stack_trace_t *trace LOCAL = jit_stack_trace(); for (int i = 0; i < trace->count; i++) { tree_t where = tree_from_object(trace->frames[i].object); if (where == NULL) diff --git a/src/lower.c b/src/lower.c index e1e18a12..2afd838f 100644 --- a/src/lower.c +++ b/src/lower.c @@ -12871,13 +12871,18 @@ unit_registry_t *unit_registry_new(void) void unit_registry_free(unit_registry_t *ur) { + vcode_close(); + const void *key; void *value; for (hash_iter_t it = HASH_BEGIN; hash_iter(ur->map, &it, &key, &value); ) { switch (pointer_tag(value)) { case UNIT_FINALISED: - // Let this leak for now as it may be referenced from the library + { + vcode_unit_t vu = untag_pointer(value, struct _vcode_unit); + vcode_unit_unref(vu); + } break; case UNIT_GENERATED: @@ -12885,6 +12890,7 @@ void unit_registry_free(unit_registry_t *ur) lower_unit_t *lu = untag_pointer(value, lower_unit_t); assert(lu->finished); assert(lu->registry == ur); + vcode_unit_unref(lu->vunit); lower_unit_free(lu); } break; diff --git a/src/rt/model.c b/src/rt/model.c index 956dc6b6..b5c80771 100644 --- a/src/rt/model.c +++ b/src/rt/model.c @@ -627,6 +627,12 @@ static void cleanup_nexus(rt_model_t *m, rt_nexus_t *n) break; case SOURCE_PORT: + if (s->u.port.conv_func != NULL) { + free(s->u.port.conv_func->inputs); + s->u.port.conv_func->inputs = NULL; + } + break; + case SOURCE_FORCING: case SOURCE_DEPOSIT: break; diff --git a/src/vcode.c b/src/vcode.c index 169dfebf..80efa776 100644 --- a/src/vcode.c +++ b/src/vcode.c @@ -532,8 +532,11 @@ void vcode_unit_unref(vcode_unit_t unit) if (unit == active_unit) vcode_close(); - while (unit->children) - vcode_unit_unref(unit->children); + for (vcode_unit_t it = unit->children; it != NULL; it = it->next) { + assert(it->context == unit); + it->context = NULL; + } + unit->children = NULL; if (unit->context != NULL) { vcode_unit_t *it = &(unit->context->children); -- 2.39.2