From 46e0bf78018d2feaaf8d3cfbd32cdf09fe84c191 Mon Sep 17 00:00:00 2001 From: pyscripter Date: Wed, 13 Mar 2024 17:41:56 +0100 Subject: [PATCH] Fix reference counting in ProcessSystemExit. See https://en.delphipraxis.net/topic/11182-tpydelphiwrapper-memory-leaks/. --- Source/PythonEngine.pas | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 47dc6eda..23e23eb8 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -6425,15 +6425,19 @@ procedure TPythonEngine.ListToSet( List : PPyObject; data : Pointer; size : Inte end; procedure TPythonEngine.CheckError(ACatchStopEx : Boolean = False); + procedure ProcessSystemExit; var errtype, errvalue, errtraceback: PPyObject; SErrValue: string; begin + // PyErr_Fetch clears the error. The returned python objects are new references PyErr_Fetch(errtype, errvalue, errtraceback); Traceback.Refresh(errtraceback); SErrValue := PyObjectAsString(errvalue); - PyErr_Clear; + Py_XDECREF(errtype); + Py_XDECREF(errvalue); + Py_XDECREF(errtraceback); raise EPySystemExit.CreateResFmt(@SPyExcSystemError, [SErrValue]); end;