From 4a8fac9147627c6750e0a751cf2317fcb50c0231 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 11 Dec 2020 12:05:28 +0100 Subject: [PATCH] Part: [skip ci] fix memory leaks + add convenience function GeometryExtension::copyPyObject() + make sure to destroy the clone when leaving getGeometry() --- src/Mod/Part/App/GeometryExtension.cpp | 8 ++++++++ src/Mod/Part/App/GeometryExtension.h | 1 + src/Mod/Part/App/GeometryPyImp.cpp | 12 +++--------- .../Sketcher/App/ExternalGeometryFacadePyImp.cpp | 15 +++++---------- src/Mod/Sketcher/App/GeometryFacadePyImp.cpp | 16 +++++----------- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/Mod/Part/App/GeometryExtension.cpp b/src/Mod/Part/App/GeometryExtension.cpp index 6177062098..fd8c6fad0c 100644 --- a/src/Mod/Part/App/GeometryExtension.cpp +++ b/src/Mod/Part/App/GeometryExtension.cpp @@ -28,6 +28,7 @@ #include #include "GeometryExtension.h" +#include "GeometryExtensionPy.h" using namespace Part; @@ -37,6 +38,13 @@ GeometryExtension::GeometryExtension() { } +PyObject* GeometryExtension::copyPyObject() const +{ + Py::Tuple tuple; + Py::Object obj = Py::asObject(const_cast(this)->getPyObject()); + return static_cast(obj.ptr())->copy(tuple.ptr()); +} + TYPESYSTEM_SOURCE_ABSTRACT(Part::GeometryPersistenceExtension,Part::GeometryExtension) void GeometryPersistenceExtension::restoreNameAttribute(Base::XMLReader &reader) diff --git a/src/Mod/Part/App/GeometryExtension.h b/src/Mod/Part/App/GeometryExtension.h index d4541f2595..02119670ab 100644 --- a/src/Mod/Part/App/GeometryExtension.h +++ b/src/Mod/Part/App/GeometryExtension.h @@ -41,6 +41,7 @@ public: virtual std::unique_ptr copy(void) const = 0; virtual PyObject *getPyObject(void) = 0; + PyObject* copyPyObject() const; inline void setName(const std::string& str) {name = str;} inline const std::string &getName () const {return name;} diff --git a/src/Mod/Part/App/GeometryPyImp.cpp b/src/Mod/Part/App/GeometryPyImp.cpp index 4f0fffe730..1598408c09 100644 --- a/src/Mod/Part/App/GeometryPyImp.cpp +++ b/src/Mod/Part/App/GeometryPyImp.cpp @@ -248,9 +248,7 @@ PyObject* GeometryPy::getExtensionOfType(PyObject *args) std::shared_ptr ext(this->getGeometryPtr()->getExtension(type)); // we create a copy and transfer this copy's memory management responsibility to Python - Py::Tuple tuple; - PyObject* cpy = static_cast(std::const_pointer_cast(ext)->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = ext->copyPyObject(); return cpy; } catch(const Base::ValueError& e) { @@ -287,9 +285,7 @@ PyObject* GeometryPy::getExtensionOfName(PyObject *args) std::shared_ptr ext(this->getGeometryPtr()->getExtension(std::string(o))); // we create a copy and transfer this copy's memory management responsibility to Python - Py::Tuple tuple; - PyObject* cpy = static_cast(std::const_pointer_cast(ext)->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = ext->copyPyObject(); return cpy; } catch(const Base::ValueError& e) { @@ -427,9 +423,7 @@ PyObject* GeometryPy::getExtensions(PyObject *args) // we create a python copy and add it to the list try { - Py::Tuple tuple; - PyObject* cpy = static_cast(p->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = p->copyPyObject(); PyList_Append( list, cpy); Py_DECREF(cpy); } diff --git a/src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp b/src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp index 57bc771906..a86bce0e22 100644 --- a/src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp +++ b/src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp @@ -305,9 +305,7 @@ PyObject* ExternalGeometryFacadePy::getExtensionOfType(PyObject *args) std::shared_ptr ext(this->getExternalGeometryFacadePtr()->getExtension(type)); // we create a copy and transfer this copy's memory management responsibility to Python - Py::Tuple tuple; - PyObject* cpy = static_cast(std::const_pointer_cast(ext)->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = ext->copyPyObject(); return cpy; } catch(const Base::ValueError& e) { @@ -344,9 +342,7 @@ PyObject* ExternalGeometryFacadePy::getExtensionOfName(PyObject *args) std::shared_ptr ext(this->getExternalGeometryFacadePtr()->getExtension(std::string(o))); // we create a copy and transfer this copy's memory management responsibility to Python - Py::Tuple tuple; - PyObject* cpy = static_cast(std::const_pointer_cast(ext)->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = ext->copyPyObject(); return cpy; } catch(const Base::ValueError& e) { @@ -482,9 +478,7 @@ PyObject* ExternalGeometryFacadePy::getExtensions(PyObject *args) if(p) { // we create a python copy and add it to the list try { - Py::Tuple tuple; - PyObject* cpy = static_cast(std::const_pointer_cast(p)->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = p->copyPyObject(); PyList_Append( list, cpy); Py_DECREF(cpy); } @@ -523,7 +517,8 @@ Py::String ExternalGeometryFacadePy::getTag(void) const Py::Object ExternalGeometryFacadePy::getGeometry(void) const { // We return a clone - return Py::Object(getExternalGeometryFacadePtr()->getGeometry()->clone()->getPyObject(),true); + std::unique_ptr geo(getExternalGeometryFacadePtr()->getGeometry()->clone()); + return Py::Object(geo->getPyObject(), true); } void ExternalGeometryFacadePy::setGeometry(Py::Object arg) diff --git a/src/Mod/Sketcher/App/GeometryFacadePyImp.cpp b/src/Mod/Sketcher/App/GeometryFacadePyImp.cpp index 7ea43e12ad..5ddf5885a4 100644 --- a/src/Mod/Sketcher/App/GeometryFacadePyImp.cpp +++ b/src/Mod/Sketcher/App/GeometryFacadePyImp.cpp @@ -284,9 +284,7 @@ PyObject* GeometryFacadePy::getExtensionOfType(PyObject *args) std::shared_ptr ext(this->getGeometryFacadePtr()->getExtension(type)); // we create a copy and transfer this copy's memory management responsibility to Python - Py::Tuple tuple; - PyObject* cpy = static_cast(std::const_pointer_cast(ext)->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = ext->copyPyObject(); return cpy; } catch(const Base::ValueError& e) { @@ -323,9 +321,7 @@ PyObject* GeometryFacadePy::getExtensionOfName(PyObject *args) std::shared_ptr ext(this->getGeometryFacadePtr()->getExtension(std::string(o))); // we create a copy and transfer this copy's memory management responsibility to Python - Py::Tuple tuple; - PyObject* cpy = static_cast(std::const_pointer_cast(ext)->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = ext->copyPyObject(); return cpy; } catch(const Base::ValueError& e) { @@ -459,11 +455,8 @@ PyObject* GeometryFacadePy::getExtensions(PyObject *args) if(p) { // we create a python copy and add it to the list - Py::Tuple tuple; - try { - PyObject* cpy = static_cast(std::const_pointer_cast(p)->getPyObject())->copy(tuple.ptr()); - + PyObject* cpy = p->copyPyObject(); PyList_Append( list, cpy); Py_DECREF(cpy); } @@ -501,7 +494,8 @@ Py::String GeometryFacadePy::getTag(void) const Py::Object GeometryFacadePy::getGeometry(void) const { // We return a clone - return Py::Object(getGeometryFacadePtr()->getGeometry()->clone()->getPyObject(),true); + std::unique_ptr geo(getGeometryFacadePtr()->getGeometry()->clone()); + return Py::Object(geo->getPyObject(), true); } void GeometryFacadePy::setGeometry(Py::Object arg)