changeset 118:7f8f26ef5006

fix memory leaks
author Alain Mazy <am@osimis.io>
date Mon, 28 Aug 2023 16:44:41 +0200
parents 1029689d7b6e
children cf6decdf9e15
files NEWS Sources/DicomScpCallbacks.cpp Sources/IncomingHttpRequestFilter.cpp
diffstat 3 files changed, 44 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Mon Aug 28 16:18:37 2023 +0200
+++ b/NEWS	Mon Aug 28 16:44:41 2023 +0200
@@ -2,7 +2,8 @@
 ===============================
 
 Maintenance:
-* Fix memory leaks when a python script calls orthanc.RestApiPost() and sibling methods
+* Fix memory leaks when a python script calls orthanc.RestApiPost() and sibling methods,
+  in IncomingHttpRequestFilter and in the CMove callback.
 * New builders for Windows: Supporting 32 / 64bit with Python 3.9 / 3.10 / 3.11
 
 
--- a/Sources/DicomScpCallbacks.cpp	Mon Aug 28 16:18:37 2023 +0200
+++ b/Sources/DicomScpCallbacks.cpp	Mon Aug 28 16:44:41 2023 +0200
@@ -293,52 +293,35 @@
           throw OrthancPlugins::PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
       }
 
-      {
-        PythonString tmp(lock, level);
-        PyDict_SetItemString(kw.GetPyObject(), "Level", tmp.Release());
-      }
+      PythonString pyLevel(lock, level);
+      PyDict_SetItemString(kw.GetPyObject(), "Level", pyLevel.GetPyObject());
 
-      {
-        PythonString tmp(lock, patientId_);
-        PyDict_SetItemString(kw.GetPyObject(), "PatientID", tmp.Release());
-      }
+      PythonString pyPatientId(lock, patientId_);
+      PyDict_SetItemString(kw.GetPyObject(), "PatientID", pyPatientId.GetPyObject());
 
-      {
-        PythonString tmp(lock, accessionNumber_);
-        PyDict_SetItemString(kw.GetPyObject(), "AccessionNumber", tmp.Release());
-      }
+      PythonString pyAccessionNumber(lock, accessionNumber_);
+      PyDict_SetItemString(kw.GetPyObject(), "AccessionNumber", pyAccessionNumber.GetPyObject());
 
-      {
-        PythonString tmp(lock, studyInstanceUid_);
-        PyDict_SetItemString(kw.GetPyObject(), "StudyInstanceUID", tmp.Release());
-      }
+      PythonString pyStudyInstanceUid(lock, studyInstanceUid_);
+      PyDict_SetItemString(kw.GetPyObject(), "StudyInstanceUID", pyStudyInstanceUid.GetPyObject());
+
+      PythonString pySeriesInstanceUid(lock, seriesInstanceUid_);
+      PyDict_SetItemString(kw.GetPyObject(), "SeriesInstanceUID", pySeriesInstanceUid.GetPyObject());
 
-      {
-        PythonString tmp(lock, seriesInstanceUid_);
-        PyDict_SetItemString(kw.GetPyObject(), "SeriesInstanceUID", tmp.Release());
-      }
+      PythonString pySopInstanceUid(lock, sopInstanceUid_);
+      PyDict_SetItemString(kw.GetPyObject(), "SOPInstanceUID", pySopInstanceUid.GetPyObject());
 
-      {
-        PythonString tmp(lock, sopInstanceUid_);
-        PyDict_SetItemString(kw.GetPyObject(), "SOPInstanceUID", tmp.Release());
-      }
+      PythonString pyOriginatorAet(lock, originatorAet_);
+      PyDict_SetItemString(kw.GetPyObject(), "OriginatorAET", pyOriginatorAet.GetPyObject());
 
-      {
-        PythonString tmp(lock, originatorAet_);
-        PyDict_SetItemString(kw.GetPyObject(), "OriginatorAET", tmp.Release());
-      }
+      PythonString pySourceAet(lock, sourceAet_);
+      PyDict_SetItemString(kw.GetPyObject(), "SourceAET", pySourceAet.GetPyObject());
 
-      {
-        PythonString tmp(lock, sourceAet_);
-        PyDict_SetItemString(kw.GetPyObject(), "SourceAET", tmp.Release());
-      }
+      PythonString pyTargetAet(lock, targetAet_);
+      PyDict_SetItemString(kw.GetPyObject(), "TargetAET", pyTargetAet.GetPyObject());
 
-      {
-        PythonString tmp(lock, targetAet_);
-        PyDict_SetItemString(kw.GetPyObject(), "TargetAET", tmp.Release());
-      }
-
-      PyDict_SetItemString(kw.GetPyObject(), "OriginatorID", PyLong_FromUnsignedLong(originatorId_));
+      PythonObject pyOriginatorId(lock, PyLong_FromUnsignedLong(originatorId_));
+      PyDict_SetItemString(kw.GetPyObject(), "OriginatorID", pyOriginatorId.GetPyObject());
 
       PythonObject args(lock, PyTuple_New(0));
 
--- a/Sources/IncomingHttpRequestFilter.cpp	Mon Aug 28 16:18:37 2023 +0200
+++ b/Sources/IncomingHttpRequestFilter.cpp	Mon Aug 28 16:44:41 2023 +0200
@@ -51,36 +51,40 @@
     }
 
     PythonObject kw(lock, PyDict_New());
-    PyDict_SetItemString(kw.GetPyObject(), "method", PyLong_FromLong(method));
+
+    PythonObject pyMethod(lock, PyLong_FromUnsignedLong(method));
+    PyDict_SetItemString(kw.GetPyObject(), "method", pyMethod.GetPyObject());
 
+    PythonString str(lock, ip);
+    PyDict_SetItemString(kw.GetPyObject(), "ip", str.GetPyObject());
+
+    PythonObject headers(lock, PyDict_New());
+    std::vector<boost::shared_ptr<PythonString> > pyHeaderValues;
+
+    for (uint32_t i = 0; i < headersCount; i++)
     {
-      PythonString str(lock, ip);
-      PyDict_SetItemString(kw.GetPyObject(), "ip", str.Release());
+      boost::shared_ptr<PythonString> value(new PythonString(lock, headersValues[i]));
+      PyDict_SetItemString(headers.GetPyObject(), headersKeys[i], value->GetPyObject());
+      pyHeaderValues.push_back(value);
     }
 
-    {
-      PythonObject headers(lock, PyDict_New());
+    PyDict_SetItemString(kw.GetPyObject(), "headers", headers.GetPyObject());
 
-      for (uint32_t i = 0; i < headersCount; i++)
-      {
-        PythonString str(lock, headersValues[i]);
-        PyDict_SetItemString(headers.GetPyObject(), headersKeys[i], str.Release());
-      }
-
-      PyDict_SetItemString(kw.GetPyObject(), "headers", headers.Release());
-    }
+    std::unique_ptr<PythonObject> pyGetArguments;
+    std::vector<boost::shared_ptr<PythonString> > pyGetValues;
 
     if (method == OrthancPluginHttpMethod_Get)
     {
-      PythonObject getArguments(lock, PyDict_New());
+      pyGetArguments.reset(new PythonObject(lock, PyDict_New()));
 
       for (uint32_t i = 0; i < getArgumentsCount; i++)
       {
-        PythonString str(lock, getArgumentsValues[i]);
-        PyDict_SetItemString(getArguments.GetPyObject(), getArgumentsKeys[i], str.Release());
+        boost::shared_ptr<PythonString> value(new PythonString(lock, getArgumentsValues[i]));
+        PyDict_SetItemString(pyGetArguments->GetPyObject(), getArgumentsKeys[i], value->GetPyObject());
+        pyGetValues.push_back(value);
       }
 
-      PyDict_SetItemString(kw.GetPyObject(), "get", getArguments.Release());
+      PyDict_SetItemString(kw.GetPyObject(), "get", pyGetArguments->GetPyObject());
     }
     
     PythonObject result(lock, PyObject_Call(incomingHttpRequestFilter_,