From 85d3d9eec0677bf6f5984249ae13a534dbcbdaef Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 9 May 2024 00:03:01 +0200 Subject: [PATCH] TD: Fix memory leaks with PySequence_GetItem --- src/Mod/TechDraw/App/DrawViewPartPyImp.cpp | 11 +++++------ src/Mod/TechDraw/App/PropertyCenterLineList.cpp | 11 ++++++----- src/Mod/TechDraw/App/PropertyCosmeticEdgeList.cpp | 11 ++++++----- src/Mod/TechDraw/App/PropertyCosmeticVertexList.cpp | 11 ++++++----- src/Mod/TechDraw/App/PropertyGeomFormatList.cpp | 11 ++++++----- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawViewPartPyImp.cpp b/src/Mod/TechDraw/App/DrawViewPartPyImp.cpp index 512e9b63ff..cc38259166 100644 --- a/src/Mod/TechDraw/App/DrawViewPartPyImp.cpp +++ b/src/Mod/TechDraw/App/DrawViewPartPyImp.cpp @@ -314,15 +314,14 @@ PyObject* DrawViewPartPy::removeCosmeticVertex(PyObject *args) } if (PySequence_Check(pDelList)) { - Py_ssize_t nSize = PySequence_Size(pDelList); - for (Py_ssize_t i=0; i < nSize; i++) { - PyObject* item = PySequence_GetItem(pDelList, i); - if (!PyObject_TypeCheck(item, &(TechDraw::CosmeticVertexPy::Type))) { + Py::Sequence sequence(pDelList); + for (const auto& item : sequence) { + if (!PyObject_TypeCheck(item.ptr(), &(TechDraw::CosmeticVertexPy::Type))) { PyErr_Format(PyExc_TypeError ,"Types in sequence must be 'CosmeticVertex', not %s", - Py_TYPE(item)->tp_name); + Py_TYPE(item.ptr())->tp_name); return nullptr; } - TechDraw::CosmeticVertexPy* cvPy = static_cast(item); + TechDraw::CosmeticVertexPy* cvPy = static_cast(item.ptr()); TechDraw::CosmeticVertex* cv = cvPy->getCosmeticVertexPtr(); dvp->removeCosmeticVertex(cv->getTagAsString()); } diff --git a/src/Mod/TechDraw/App/PropertyCenterLineList.cpp b/src/Mod/TechDraw/App/PropertyCenterLineList.cpp index 7d635dfe68..e1e99217aa 100644 --- a/src/Mod/TechDraw/App/PropertyCenterLineList.cpp +++ b/src/Mod/TechDraw/App/PropertyCenterLineList.cpp @@ -95,19 +95,20 @@ PyObject *PropertyCenterLineList::getPyObject() void PropertyCenterLineList::setPyObject(PyObject *value) { if (PySequence_Check(value)) { - Py_ssize_t nSize = PySequence_Size(value); + Py::Sequence sequence(value); + Py_ssize_t nSize = sequence.size(); std::vector values; values.resize(nSize); for (Py_ssize_t i=0; i < nSize; ++i) { - PyObject* item = PySequence_GetItem(value, i); - if (!PyObject_TypeCheck(item, &(CenterLinePy::Type))) { + Py::Object item = sequence.getItem(i); + if (!PyObject_TypeCheck(item.ptr(), &(CenterLinePy::Type))) { std::string error = std::string("types in list must be 'CenterLine', not "); - error += item->ob_type->tp_name; + error += item.ptr()->ob_type->tp_name; throw Base::TypeError(error); } - values[i] = static_cast(item)->getCenterLinePtr(); + values[i] = static_cast(item.ptr())->getCenterLinePtr(); } setValues(values); diff --git a/src/Mod/TechDraw/App/PropertyCosmeticEdgeList.cpp b/src/Mod/TechDraw/App/PropertyCosmeticEdgeList.cpp index 46b60a76cd..b264b6dd44 100644 --- a/src/Mod/TechDraw/App/PropertyCosmeticEdgeList.cpp +++ b/src/Mod/TechDraw/App/PropertyCosmeticEdgeList.cpp @@ -99,19 +99,20 @@ PyObject *PropertyCosmeticEdgeList::getPyObject() void PropertyCosmeticEdgeList::setPyObject(PyObject *value) { if (PySequence_Check(value)) { - Py_ssize_t nSize = PySequence_Size(value); + Py::Sequence sequence(value); + Py_ssize_t nSize = sequence.size(); std::vector values; values.resize(nSize); for (Py_ssize_t i=0; i < nSize; ++i) { - PyObject* item = PySequence_GetItem(value, i); - if (!PyObject_TypeCheck(item, &(CosmeticEdgePy::Type))) { + Py::Object item = sequence.getItem(i); + if (!PyObject_TypeCheck(item.ptr(), &(CosmeticEdgePy::Type))) { std::string error = std::string("types in list must be 'CosmeticEdge', not "); - error += item->ob_type->tp_name; + error += item.ptr()->ob_type->tp_name; throw Base::TypeError(error); } - values[i] = static_cast(item)->getCosmeticEdgePtr(); + values[i] = static_cast(item.ptr())->getCosmeticEdgePtr(); } setValues(values); diff --git a/src/Mod/TechDraw/App/PropertyCosmeticVertexList.cpp b/src/Mod/TechDraw/App/PropertyCosmeticVertexList.cpp index ced5eb181e..451c0f3322 100644 --- a/src/Mod/TechDraw/App/PropertyCosmeticVertexList.cpp +++ b/src/Mod/TechDraw/App/PropertyCosmeticVertexList.cpp @@ -97,19 +97,20 @@ void PropertyCosmeticVertexList::setPyObject(PyObject *value) // check container of this property to notify about changes if (PySequence_Check(value)) { - Py_ssize_t nSize = PySequence_Size(value); + Py::Sequence sequence(value); + Py_ssize_t nSize = sequence.size(); std::vector values; values.resize(nSize); for (Py_ssize_t i=0; i < nSize; ++i) { - PyObject* item = PySequence_GetItem(value, i); - if (!PyObject_TypeCheck(item, &(CosmeticVertexPy::Type))) { + Py::Object item = sequence.getItem(i); + if (!PyObject_TypeCheck(item.ptr(), &(CosmeticVertexPy::Type))) { std::string error = std::string("types in list must be 'CosmeticVertex', not "); - error += item->ob_type->tp_name; + error += item.ptr()->ob_type->tp_name; throw Base::TypeError(error); } - values[i] = static_cast(item)->getCosmeticVertexPtr(); + values[i] = static_cast(item.ptr())->getCosmeticVertexPtr(); } setValues(values); diff --git a/src/Mod/TechDraw/App/PropertyGeomFormatList.cpp b/src/Mod/TechDraw/App/PropertyGeomFormatList.cpp index fca52e0258..3bdb97714c 100644 --- a/src/Mod/TechDraw/App/PropertyGeomFormatList.cpp +++ b/src/Mod/TechDraw/App/PropertyGeomFormatList.cpp @@ -107,19 +107,20 @@ void PropertyGeomFormatList::setPyObject(PyObject *value) // Part2DObject* part2d = dynamic_cast(this->getContainer()); if (PySequence_Check(value)) { - Py_ssize_t nSize = PySequence_Size(value); + Py::Sequence sequence(value); + Py_ssize_t nSize = sequence.size(); std::vector values; values.resize(nSize); for (Py_ssize_t i=0; i < nSize; ++i) { - PyObject* item = PySequence_GetItem(value, i); - if (!PyObject_TypeCheck(item, &(GeomFormatPy::Type))) { + Py::Object item = sequence.getItem(i); + if (!PyObject_TypeCheck(item.ptr(), &(GeomFormatPy::Type))) { std::string error = std::string("types in list must be 'GeomFormat', not "); - error += item->ob_type->tp_name; + error += item.ptr()->ob_type->tp_name; throw Base::TypeError(error); } - values[i] = static_cast(item)->getGeomFormatPtr(); + values[i] = static_cast(item.ptr())->getGeomFormatPtr(); } setValues(values);