From 7567cc11c9d6a12b59661f2729c2aee2d0012d53 Mon Sep 17 00:00:00 2001 From: baldurk Date: Wed, 18 Mar 2026 13:34:19 +0000 Subject: [PATCH] Fix GC collection of python contexts --- qrenderdoc/Code/pyrenderdoc/PythonContext.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp b/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp index d65091fd7..7208d1e94 100644 --- a/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp +++ b/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp @@ -514,8 +514,7 @@ PythonContext::PythonContext(QObject *parent) : QObject(parent) PythonContext::~PythonContext() { PyGILState_STATE gil = PyGILState_Ensure(); - if(m_Completer) - Py_DecRef(m_Completer); + PyGILState_Release(gil); // do a final tick to gather any remaining output @@ -566,9 +565,23 @@ void PythonContext::Finish() { PyGILState_STATE gil = PyGILState_Ensure(); + if(m_Completer) + { + Py_DecRef(m_Completer); + m_Completer = NULL; + } + // release our external handle to globals. It'll now only be ref'd from inside Py_XDECREF(context_namespace); + // force a GC to detect the cycle left + PyObject *gc = PyImport_ImportModule("gc"); + if(gc) + { + Py_XDECREF(PyObject_CallMethod(gc, "collect", NULL)); + Py_XDECREF(gc); + } + PyGILState_Release(gil); }