Part: [skip ci] fix memory leaks
+ add convenience function GeometryExtension::copyPyObject() + make sure to destroy the clone when leaving getGeometry()
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
#include <Base/Tools.h>
|
||||
|
||||
#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<GeometryExtension*>(this)->getPyObject());
|
||||
return static_cast<GeometryExtensionPy *>(obj.ptr())->copy(tuple.ptr());
|
||||
}
|
||||
|
||||
TYPESYSTEM_SOURCE_ABSTRACT(Part::GeometryPersistenceExtension,Part::GeometryExtension)
|
||||
|
||||
void GeometryPersistenceExtension::restoreNameAttribute(Base::XMLReader &reader)
|
||||
|
||||
@@ -41,6 +41,7 @@ public:
|
||||
virtual std::unique_ptr<GeometryExtension> 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;}
|
||||
|
||||
@@ -248,9 +248,7 @@ PyObject* GeometryPy::getExtensionOfType(PyObject *args)
|
||||
std::shared_ptr<const GeometryExtension> 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<GeometryExtensionPy *>(std::const_pointer_cast<GeometryExtension>(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<const GeometryExtension> 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<GeometryExtensionPy *>(std::const_pointer_cast<GeometryExtension>(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<GeometryExtensionPy *>(p->getPyObject())->copy(tuple.ptr());
|
||||
|
||||
PyObject* cpy = p->copyPyObject();
|
||||
PyList_Append( list, cpy);
|
||||
Py_DECREF(cpy);
|
||||
}
|
||||
|
||||
@@ -305,9 +305,7 @@ PyObject* ExternalGeometryFacadePy::getExtensionOfType(PyObject *args)
|
||||
std::shared_ptr<const Part::GeometryExtension> 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<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(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<const Part::GeometryExtension> 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<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(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<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(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<Part::Geometry> geo(getExternalGeometryFacadePtr()->getGeometry()->clone());
|
||||
return Py::Object(geo->getPyObject(), true);
|
||||
}
|
||||
|
||||
void ExternalGeometryFacadePy::setGeometry(Py::Object arg)
|
||||
|
||||
@@ -284,9 +284,7 @@ PyObject* GeometryFacadePy::getExtensionOfType(PyObject *args)
|
||||
std::shared_ptr<const Part::GeometryExtension> 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<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(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<const Part::GeometryExtension> 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<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(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<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(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<Part::Geometry> geo(getGeometryFacadePtr()->getGeometry()->clone());
|
||||
return Py::Object(geo->getPyObject(), true);
|
||||
}
|
||||
|
||||
void GeometryFacadePy::setGeometry(Py::Object arg)
|
||||
|
||||
Reference in New Issue
Block a user