# HG changeset patch # User Alain Mazy # Date 1693233881 -7200 # Node ID 7f8f26ef50065e505ed9785f56589d4a015fc782 # Parent 1029689d7b6e39bc254c0beffa151312a82c919b fix memory leaks diff -r 1029689d7b6e -r 7f8f26ef5006 NEWS --- 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 diff -r 1029689d7b6e -r 7f8f26ef5006 Sources/DicomScpCallbacks.cpp --- 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)); diff -r 1029689d7b6e -r 7f8f26ef5006 Sources/IncomingHttpRequestFilter.cpp --- 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 > pyHeaderValues; + + for (uint32_t i = 0; i < headersCount; i++) { - PythonString str(lock, ip); - PyDict_SetItemString(kw.GetPyObject(), "ip", str.Release()); + boost::shared_ptr 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 pyGetArguments; + std::vector > 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 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_,