From ae7b645b7a4919c20c75f68348347038601229f7 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Wed, 10 Sep 2025 02:50:45 -0700 Subject: [PATCH] Fix root being possibly uncounted in check_gc_root --- src/refcount.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/refcount.c b/src/refcount.c index 21f893b..1bb233a 100644 --- a/src/refcount.c +++ b/src/refcount.c @@ -648,7 +648,11 @@ static ptrdiff_t check_gc_root(RefcountContext *ctx, RefcountList **root_ptr) { *root_ptr = (*root_ptr)->next; return -1; } - size_t seen_objects = 0; + // initialize information for the root + ht_insert( + counts, root->data, + HT_STUFF( + REFCOUNT_OBJECT_ENTRY(ctx, root->data)->impl.counted.ref_count)); size_t clear_objects = 0; // ignore allocation errors until I decide how to deal with them (in the far // future) @@ -663,7 +667,6 @@ static ptrdiff_t check_gc_root(RefcountContext *ctx, RefcountList **root_ptr) { count = HT_UUNSTUFF(ht_get(counts, obj)); } else { count = REFCOUNT_OBJECT_ENTRY(ctx, obj)->impl.counted.ref_count; - ++seen_objects; // don't recuse into objects multiple times obj_held_refs(ctx, obj, &queue); } @@ -674,6 +677,7 @@ static ptrdiff_t check_gc_root(RefcountContext *ctx, RefcountList **root_ptr) { } } } + size_t seen_objects = ht_count(counts); ptrdiff_t freed_count = 0; if (seen_objects == clear_objects && !call_destructors_for_gc(ctx, counts)) {