From 18b231f8d61d661e88599ee5458aa700a58756c2 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 5 Jun 2026 15:04:02 +0200 Subject: [PATCH 1/2] gh-146527: Fix memory leak in _PyGC_Fini() Free generation_stats allocated by _PyGC_Init(). Fix Python/gc.c: Python/gc_free_threading.c was already fixed. --- Python/gc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Python/gc.c b/Python/gc.c index 54ac1b089e503d0..31fdaf3d09a632a 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -1876,6 +1876,8 @@ _PyGC_Fini(PyInterpreterState *interp) GCState *gcstate = &interp->gc; Py_CLEAR(gcstate->garbage); Py_CLEAR(gcstate->callbacks); + PyMem_Free(gcstate->generation_stats); + gcstate->generation_stats = NULL; /* Prevent a subtle bug that affects sub-interpreters that use basic * single-phase init extensions (m_size == -1). Those extensions cause objects From 5b8c03f3d133feff56baada23a7be90e1de1850b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 5 Jun 2026 15:12:54 +0200 Subject: [PATCH 2/2] Oops, use PyMem_RawFree() instead of PyMem_Free() --- Python/gc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/gc.c b/Python/gc.c index 31fdaf3d09a632a..201c621bcc3cb9b 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -1876,7 +1876,7 @@ _PyGC_Fini(PyInterpreterState *interp) GCState *gcstate = &interp->gc; Py_CLEAR(gcstate->garbage); Py_CLEAR(gcstate->callbacks); - PyMem_Free(gcstate->generation_stats); + PyMem_RawFree(gcstate->generation_stats); gcstate->generation_stats = NULL; /* Prevent a subtle bug that affects sub-interpreters that use basic