From aedd829e61346d293b3a1997e28e28b915cc8473 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 6 Nov 2024 22:56:10 +0100 Subject: [PATCH] Fix DUMP_LEAKS memory leak false positive (#655) Run DUMP_LEAKS after finalizers run; they call js_free_rt too. Fixes: https://github.com/quickjs-ng/quickjs/issues/654 --- quickjs.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/quickjs.c b/quickjs.c index 6c073b18..a104aaf6 100644 --- a/quickjs.c +++ b/quickjs.c @@ -2217,6 +2217,16 @@ void JS_FreeRuntime(JSRuntime *rt) if (rt->rt_info) printf("\n"); } +#endif + + while (rt->finalizers) { + JSRuntimeFinalizerState *fs = rt->finalizers; + rt->finalizers = fs->next; + fs->finalizer(rt, fs->arg); + js_free_rt(rt, fs); + } + +#ifdef DUMP_LEAKS if (check_dump_flag(rt, DUMP_LEAKS)) { JSMallocState *s = &rt->malloc_state; if (s->malloc_count > 1) { @@ -2229,13 +2239,6 @@ void JS_FreeRuntime(JSRuntime *rt) } #endif - while (rt->finalizers) { - JSRuntimeFinalizerState *fs = rt->finalizers; - rt->finalizers = fs->next; - fs->finalizer(rt, fs->arg); - js_free_rt(rt, fs); - } - // FinalizationRegistry finalizers have run, no objects should remain assert(list_empty(&rt->gc_obj_list));