From 75bdea5155a8f7b61c3af009d0eef3ca39cde66e Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 6 Sep 2024 00:15:17 -0500 Subject: [PATCH] Fix memory leak with weak table frees. The backing buffer for weak arrays and tables was not freed upon being garbage collected. This shows up in traces and valgrind. Verified by running `make valtest` with changes. --- src/core/ev.c | 6 ++++++ src/core/gc.c | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/core/ev.c b/src/core/ev.c index 03a4b623..9c457539 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -255,6 +255,12 @@ static void add_timeout(JanetTimeout to) { void janet_async_end(JanetFiber *fiber) { if (fiber->ev_callback) { + if (fiber->ev_stream->read_fiber == fiber) { + fiber->ev_stream->read_fiber = NULL; + } + if (fiber->ev_stream->write_fiber == fiber) { + fiber->ev_stream->write_fiber = NULL; + } fiber->ev_callback(fiber, JANET_ASYNC_EVENT_DEINIT); janet_gcunroot(janet_wrap_abstract(fiber->ev_stream)); fiber->ev_callback = NULL; diff --git a/src/core/gc.c b/src/core/gc.c index ba05a5a6..e9c8aef8 100644 --- a/src/core/gc.c +++ b/src/core/gc.c @@ -321,9 +321,13 @@ static void janet_deinit_block(JanetGCObject *mem) { janet_symbol_deinit(((JanetStringHead *) mem)->data); break; case JANET_MEMORY_ARRAY: + case JANET_MEMORY_ARRAY_WEAK: janet_free(((JanetArray *) mem)->data); break; case JANET_MEMORY_TABLE: + case JANET_MEMORY_TABLE_WEAKK: + case JANET_MEMORY_TABLE_WEAKV: + case JANET_MEMORY_TABLE_WEAKKV: janet_free(((JanetTable *) mem)->data); break; case JANET_MEMORY_FIBER: {