App/Gui: fix memory leaks:

+ add function to cleanup units and quantities in debug build
+ fix reference leak in PropertyVector::getPyPathValue()
+ fix reference leak in PropertyPlacement::getPyPathValue()
+ in InterpreterSingleton::init() use a static std::vector<wchar_t*> instead of a C array
  to free memory at program end
+ in MainWindow::closeEvent() explicitly delete all task watchers
+ in ReportOutputObserver constructor pass parent to QObject
+ in PropertyEditor destructor explicitly delete QItemEditorFactory
This commit is contained in:
wmayer
2021-02-27 10:56:19 +01:00
parent 5d5d05616f
commit df0f979f3b
8 changed files with 41 additions and 11 deletions

View File

@@ -340,7 +340,6 @@ Application::Application(std::map<std::string,std::string> &mConfig)
// Translate module
PyObject* pTranslateModule = (new Base::Translate)->module().ptr();
Py_INCREF(pTranslateModule);
PyModule_AddObject(pAppModule, "Qt", pTranslateModule);
//insert Units module
@@ -1499,6 +1498,23 @@ int Application::_argc;
char ** Application::_argv;
void Application::cleanupUnits()
{
try {
Base::PyGILStateLocker lock;
Py::Module mod (Py::Module("FreeCAD").getAttr("Units").ptr());
Py::List attr(mod.dir());
for (Py::List::iterator it = attr.begin(); it != attr.end(); ++it) {
mod.delAttr(Py::String(*it));
}
}
catch (Py::Exception& e) {
Base::PyGILStateLocker lock;
e.clear();
}
}
void Application::destruct(void)
{
// saving system parameter
@@ -1529,6 +1545,11 @@ void Application::destruct(void)
_pcSysParamMngr = 0;
_pcUserParamMngr = 0;
#ifdef FC_DEBUG
// Do this only in debug mode for memory leak checkers
cleanupUnits();
#endif
// not initialized or double destruct!
assert(_pcSingleton);
delete _pcSingleton;

View File

@@ -458,6 +458,8 @@ private:
/// Destructor
virtual ~Application();
static void cleanupUnits();
/** @name member for parameter */
//@{
static ParameterManager *_pcSysParamMngr;

View File

@@ -225,11 +225,11 @@ bool PropertyVector::getPyPathValue(const ObjectIdentifier &path, Py::Object &re
std::string p = path.getSubPathStr();
if (p == ".x") {
res = new QuantityPy(new Quantity(getValue().x,unit));
res = Py::asObject(new QuantityPy(new Quantity(getValue().x,unit)));
} else if(p == ".y") {
res = new QuantityPy(new Quantity(getValue().y,unit));
res = Py::asObject(new QuantityPy(new Quantity(getValue().y,unit)));
} else if(p == ".z") {
res = new QuantityPy(new Quantity(getValue().z,unit));
res = Py::asObject(new QuantityPy(new Quantity(getValue().z,unit)));
} else
return false;
return true;
@@ -678,13 +678,13 @@ bool PropertyPlacement::getPyPathValue(const ObjectIdentifier &path, Py::Object
if (p == ".Rotation.Angle") {
Base::Vector3d axis; double angle;
_cPos.getRotation().getValue(axis,angle);
res = new QuantityPy(new Quantity(Base::toDegrees(angle),Unit::Angle));
res = Py::asObject(new QuantityPy(new Quantity(Base::toDegrees(angle),Unit::Angle)));
} else if (p == ".Base.x") {
res = new QuantityPy(new Quantity(_cPos.getPosition().x,Unit::Length));
res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().x,Unit::Length)));
} else if (p == ".Base.y") {
res = new QuantityPy(new Quantity(_cPos.getPosition().y,Unit::Length));
res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().y,Unit::Length)));
} else if (p == ".Base.z") {
res = new QuantityPy(new Quantity(_cPos.getPosition().z,Unit::Length));
res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().z,Unit::Length)));
} else
return false;
return true;