changeset 130:a3f77cf16396

reverting changesets related to leaks: 32a67788a336, 1029689d7b6e, 7f8f26ef5006
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Aug 2023 11:49:59 +0200
parents cf6decdf9e15
children c55b0583084b
files Sources/DicomScpCallbacks.cpp Sources/IncomingHttpRequestFilter.cpp Sources/RestCallbacks.cpp
diffstat 3 files changed, 80 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/Sources/DicomScpCallbacks.cpp	Mon Aug 28 18:30:42 2023 +0200
+++ b/Sources/DicomScpCallbacks.cpp	Wed Aug 30 11:49:59 2023 +0200
@@ -293,35 +293,52 @@
           throw OrthancPlugins::PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
       }
 
-      PythonString pyLevel(lock, level);
-      PyDict_SetItemString(kw.GetPyObject(), "Level", pyLevel.GetPyObject());
+      {
+        PythonString tmp(lock, level);
+        PyDict_SetItemString(kw.GetPyObject(), "Level", tmp.Release());
+      }
 
-      PythonString pyPatientId(lock, patientId_);
-      PyDict_SetItemString(kw.GetPyObject(), "PatientID", pyPatientId.GetPyObject());
+      {
+        PythonString tmp(lock, patientId_);
+        PyDict_SetItemString(kw.GetPyObject(), "PatientID", tmp.Release());
+      }
 
-      PythonString pyAccessionNumber(lock, accessionNumber_);
-      PyDict_SetItemString(kw.GetPyObject(), "AccessionNumber", pyAccessionNumber.GetPyObject());
+      {
+        PythonString tmp(lock, accessionNumber_);
+        PyDict_SetItemString(kw.GetPyObject(), "AccessionNumber", 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, studyInstanceUid_);
+        PyDict_SetItemString(kw.GetPyObject(), "StudyInstanceUID", tmp.Release());
+      }
 
-      PythonString pySopInstanceUid(lock, sopInstanceUid_);
-      PyDict_SetItemString(kw.GetPyObject(), "SOPInstanceUID", pySopInstanceUid.GetPyObject());
+      {
+        PythonString tmp(lock, seriesInstanceUid_);
+        PyDict_SetItemString(kw.GetPyObject(), "SeriesInstanceUID", tmp.Release());
+      }
 
-      PythonString pyOriginatorAet(lock, originatorAet_);
-      PyDict_SetItemString(kw.GetPyObject(), "OriginatorAET", pyOriginatorAet.GetPyObject());
+      {
+        PythonString tmp(lock, sopInstanceUid_);
+        PyDict_SetItemString(kw.GetPyObject(), "SOPInstanceUID", tmp.Release());
+      }
 
-      PythonString pySourceAet(lock, sourceAet_);
-      PyDict_SetItemString(kw.GetPyObject(), "SourceAET", pySourceAet.GetPyObject());
+      {
+        PythonString tmp(lock, originatorAet_);
+        PyDict_SetItemString(kw.GetPyObject(), "OriginatorAET", tmp.Release());
+      }
 
-      PythonString pyTargetAet(lock, targetAet_);
-      PyDict_SetItemString(kw.GetPyObject(), "TargetAET", pyTargetAet.GetPyObject());
+      {
+        PythonString tmp(lock, sourceAet_);
+        PyDict_SetItemString(kw.GetPyObject(), "SourceAET", tmp.Release());
+      }
 
-      PythonObject pyOriginatorId(lock, PyLong_FromUnsignedLong(originatorId_));
-      PyDict_SetItemString(kw.GetPyObject(), "OriginatorID", pyOriginatorId.GetPyObject());
+      {
+        PythonString tmp(lock, targetAet_);
+        PyDict_SetItemString(kw.GetPyObject(), "TargetAET", tmp.Release());
+      }
+
+      PyDict_SetItemString(kw.GetPyObject(), "OriginatorID", PyLong_FromUnsignedLong(originatorId_));
 
       PythonObject args(lock, PyTuple_New(0));
 
--- a/Sources/IncomingHttpRequestFilter.cpp	Mon Aug 28 18:30:42 2023 +0200
+++ b/Sources/IncomingHttpRequestFilter.cpp	Wed Aug 30 11:49:59 2023 +0200
@@ -51,40 +51,36 @@
     }
 
     PythonObject kw(lock, PyDict_New());
-
-    PythonObject pyMethod(lock, PyLong_FromUnsignedLong(method));
-    PyDict_SetItemString(kw.GetPyObject(), "method", pyMethod.GetPyObject());
-
-    PythonString str(lock, ip);
-    PyDict_SetItemString(kw.GetPyObject(), "ip", str.GetPyObject());
+    PyDict_SetItemString(kw.GetPyObject(), "method", PyLong_FromLong(method));
 
-    PythonObject headers(lock, PyDict_New());
-    std::vector<boost::shared_ptr<PythonString> > pyHeaderValues;
-
-    for (uint32_t i = 0; i < headersCount; i++)
     {
-      boost::shared_ptr<PythonString> value(new PythonString(lock, headersValues[i]));
-      PyDict_SetItemString(headers.GetPyObject(), headersKeys[i], value->GetPyObject());
-      pyHeaderValues.push_back(value);
+      PythonString str(lock, ip);
+      PyDict_SetItemString(kw.GetPyObject(), "ip", str.Release());
     }
 
-    PyDict_SetItemString(kw.GetPyObject(), "headers", headers.GetPyObject());
+    {
+      PythonObject headers(lock, PyDict_New());
 
-    std::unique_ptr<PythonObject> pyGetArguments;
-    std::vector<boost::shared_ptr<PythonString> > pyGetValues;
+      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());
+    }
 
     if (method == OrthancPluginHttpMethod_Get)
     {
-      pyGetArguments.reset(new PythonObject(lock, PyDict_New()));
+      PythonObject getArguments(lock, PyDict_New());
 
       for (uint32_t i = 0; i < getArgumentsCount; i++)
       {
-        boost::shared_ptr<PythonString> value(new PythonString(lock, getArgumentsValues[i]));
-        PyDict_SetItemString(pyGetArguments->GetPyObject(), getArgumentsKeys[i], value->GetPyObject());
-        pyGetValues.push_back(value);
+        PythonString str(lock, getArgumentsValues[i]);
+        PyDict_SetItemString(getArguments.GetPyObject(), getArgumentsKeys[i], str.Release());
       }
 
-      PyDict_SetItemString(kw.GetPyObject(), "get", pyGetArguments->GetPyObject());
+      PyDict_SetItemString(kw.GetPyObject(), "get", getArguments.Release());
     }
     
     PythonObject result(lock, PyObject_Call(incomingHttpRequestFilter_,
--- a/Sources/RestCallbacks.cpp	Mon Aug 28 18:30:42 2023 +0200
+++ b/Sources/RestCallbacks.cpp	Wed Aug 30 11:49:59 2023 +0200
@@ -125,59 +125,53 @@
 
       PythonObject kw(lock, PyDict_New());
 
-      // We must handle memory ourselves for all members of kw that are set through PyDict_SetItem or PyDict_SetItemString.
-      // The Python documentation states that these methods do not steal references to the objects.  
-      // We have indeed observed memory leaks when not handling memory this way.
-      std::unique_ptr<PythonObject> pyGet;
-      std::vector<boost::shared_ptr<PythonString> > pyGetValues;
-      std::unique_ptr<PythonObject> pyHeaders;
-      std::vector<boost::shared_ptr<PythonString> > pyHeaderValues;
-      std::unique_ptr<PythonObject> pyBody;
-
-      PythonString pyMethod(lock, method);
-      PyDict_SetItemString(kw.GetPyObject(), "method", pyMethod.GetPyObject());
-
-      PythonObject pyGroups(lock, PyTuple_New(request->groupsCount));
-      for (uint32_t i = 0; i < request->groupsCount; i++)
       {
-        PythonString tmp(lock, request->groups[i]);
-        PyTuple_SetItem(pyGroups.GetPyObject(), i, tmp.Release()); // this PyTuple_SetItem method "steals" a reference -> no need to manage memory by ourselves !
+        PythonString tmp(lock, method);
+        PyDict_SetItemString(kw.GetPyObject(), "method", tmp.Release());
       }
 
-      PyDict_SetItemString(kw.GetPyObject(), "groups", pyGroups.GetPyObject());
+      {
+        PythonObject groups(lock, PyTuple_New(request->groupsCount));
+
+        for (uint32_t i = 0; i < request->groupsCount; i++)
+        {
+          PythonString tmp(lock, request->groups[i]);
+          PyTuple_SetItem(groups.GetPyObject(), i, tmp.Release());
+        }
+
+        PyDict_SetItemString(kw.GetPyObject(), "groups", groups.Release());
+      }
 
       if (request->method == OrthancPluginHttpMethod_Get)
       {
-        pyGet.reset(new PythonObject(lock, PyDict_New()));
+        PythonObject get(lock, PyDict_New());
+
         for (uint32_t i = 0; i < request->getCount; i++)
         {
-          boost::shared_ptr<PythonString> value(new PythonString(lock, request->getValues[i]));
-          PyDict_SetItemString(pyGet->GetPyObject(), request->getKeys[i], value->GetPyObject());
-          pyGetValues.push_back(value);
+          PythonString value(lock, request->getValues[i]);
+          PyDict_SetItemString(get.GetPyObject(), request->getKeys[i], value.Release());
         }
 
-        PyDict_SetItemString(kw.GetPyObject(), "get", pyGet->GetPyObject());
+        PyDict_SetItemString(kw.GetPyObject(), "get", get.Release());
       }
 
       {
-        pyHeaders.reset(new PythonObject(lock, PyDict_New()));
+        PythonObject headers(lock, PyDict_New());
+
         for (uint32_t i = 0; i < request->headersCount; i++)
         {
-          boost::shared_ptr<PythonString> value(new PythonString(lock, request->headersValues[i]));
-          PyDict_SetItemString(pyHeaders->GetPyObject(), request->headersKeys[i], value->GetPyObject());
-          pyHeaderValues.push_back(value);
+          PythonString value(lock, request->headersValues[i]);
+          PyDict_SetItemString(headers.GetPyObject(), request->headersKeys[i], value.Release());
         }
 
-        PyDict_SetItemString(kw.GetPyObject(), "headers", pyHeaders->GetPyObject());
+        PyDict_SetItemString(kw.GetPyObject(), "headers", headers.Release());
       }
 
       if (request->method == OrthancPluginHttpMethod_Post ||
           request->method == OrthancPluginHttpMethod_Put)
       {
-        pyBody.reset(new PythonObject(lock, PyBytes_FromStringAndSize(
-                                      reinterpret_cast<const char*>(request->body), request->bodySize)));
-
-        PyDict_SetItemString(kw.GetPyObject(), "body", pyBody->GetPyObject());
+        PyDict_SetItemString(kw.GetPyObject(), "body", PyBytes_FromStringAndSize(
+                               reinterpret_cast<const char*>(request->body), request->bodySize));
       }
 
       /**