From 13efb8a28a5f97e071a0ab35c738c3dc0262d7e7 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 13 Sep 2024 17:51:06 +0300 Subject: [PATCH] Fix memory leak in GMPy_CTXT_Exit() Closes #515 --- src/gmpy2_context.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gmpy2_context.c b/src/gmpy2_context.c index 8ef870be..fecd79af 100644 --- a/src/gmpy2_context.c +++ b/src/gmpy2_context.c @@ -184,7 +184,9 @@ GMPy_CTXT_Enter(PyObject *self, PyObject *args) static PyObject * GMPy_CTXT_Exit(PyObject *self, PyObject *args) { - int res = PyContextVar_Reset(current_context_var, ((CTXT_Object*)self)->token); + CTXT_Object *ctx = (CTXT_Object*)self; + int res = PyContextVar_Reset(current_context_var, ctx->token); + Py_DECREF(ctx->token); if (res == -1) { SYSTEM_ERROR("Unexpected failure in restoring context."); return NULL;