From 346fe8a93a4879247dc7bc87ebde912eba59026d Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 27 Apr 2021 14:32:35 +0200 Subject: [PATCH] Path: [skip ci] fix memory leaks --- src/Mod/Path/App/Tool.h | 3 +++ src/Mod/Path/App/Tooltable.cpp | 12 ++++++------ src/Mod/Path/App/Tooltable.h | 6 +++--- src/Mod/Path/App/TooltablePyImp.cpp | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Mod/Path/App/Tool.h b/src/Mod/Path/App/Tool.h index 01b3b73712..7be4d9d857 100644 --- a/src/Mod/Path/App/Tool.h +++ b/src/Mod/Path/App/Tool.h @@ -23,6 +23,7 @@ #ifndef PATH_TOOL_H #define PATH_TOOL_H +#include #include #include #include @@ -99,6 +100,8 @@ namespace Path static ToolMaterial getToolMaterial(std::string mat); static const char* MaterialName(ToolMaterial mat); }; + + using ToolPtr = std::shared_ptr; } //namespace Path #endif // PATH_TOOL_H diff --git a/src/Mod/Path/App/Tooltable.cpp b/src/Mod/Path/App/Tooltable.cpp index bc2c7e592b..504a8c09ea 100644 --- a/src/Mod/Path/App/Tooltable.cpp +++ b/src/Mod/Path/App/Tooltable.cpp @@ -47,10 +47,10 @@ Tooltable::~Tooltable() void Tooltable::addTool(const Tool &tool) { - Tool *tmp = new Tool(tool); + ToolPtr tmp = std::make_shared(tool); if (!Tools.empty()) { int max = 0; - for(std::map::const_iterator i = Tools.begin(); i != Tools.end(); ++i) { + for(std::map::const_iterator i = Tools.begin(); i != Tools.end(); ++i) { int k = i->first; if (k > max) max = k; @@ -65,7 +65,7 @@ void Tooltable::setTool(const Tool &tool, int pos) if (pos == -1) { addTool(tool); } else { - Tool *tmp = new Tool(tool); + ToolPtr tmp = std::make_shared(tool); Tools[pos] = tmp; } } @@ -88,9 +88,9 @@ void Tooltable::Save (Writer &writer) const { writer.Stream() << writer.ind() << "" << std::endl; writer.incInd(); - for(std::map::const_iterator i = Tools.begin(); i != Tools.end(); ++i) { + for(std::map::const_iterator i = Tools.begin(); i != Tools.end(); ++i) { int k = i->first; - Tool *v = i->second; + ToolPtr v = i->second; writer.Stream() << writer.ind() << "" << std::endl; writer.incInd(); v->Save(writer); @@ -110,7 +110,7 @@ void Tooltable::Restore (XMLReader &reader) for (int i = 0; i < count; i++) { reader.readElement("Toolslot"); int id = reader.getAttributeAsInteger("number"); - Tool *tmp = new Tool(); + ToolPtr tmp = std::make_shared(); tmp->Restore(reader); Tools[id] = tmp; } diff --git a/src/Mod/Path/App/Tooltable.h b/src/Mod/Path/App/Tooltable.h index 104aafde0a..bdfbe27fba 100644 --- a/src/Mod/Path/App/Tooltable.h +++ b/src/Mod/Path/App/Tooltable.h @@ -53,12 +53,12 @@ namespace Path // auto unsigned int getSize(void) const {return Tools.size();} - const Tool &getTool(int pos) {return *Tools[pos];} - const std::map &getTools(void) const {return Tools;} + const Tool &getTool(int pos) {return *Tools.at(pos);} + const std::map &getTools(void) const {return Tools;} bool hasTool(int pos) const {return (Tools.count(pos) != 0);} // attributes - std::map Tools; + std::map Tools; int Version; std::string Name; }; diff --git a/src/Mod/Path/App/TooltablePyImp.cpp b/src/Mod/Path/App/TooltablePyImp.cpp index 92da20221e..1facaab295 100644 --- a/src/Mod/Path/App/TooltablePyImp.cpp +++ b/src/Mod/Path/App/TooltablePyImp.cpp @@ -97,7 +97,7 @@ int TooltablePy::PyInit(PyObject* args, PyObject* /*kwd*/) Py::Dict TooltablePy::getTools(void) const { Py::Dict dict; - for(std::map::iterator i = getTooltablePtr()->Tools.begin(); i != getTooltablePtr()->Tools.end(); ++i) { + for(std::map::iterator i = getTooltablePtr()->Tools.begin(); i != getTooltablePtr()->Tools.end(); ++i) { PyObject *tool = new Path::ToolPy(new Tool(*i->second)); dict.setItem(Py::Long(i->first), Py::asObject(tool)); } @@ -267,7 +267,7 @@ PyObject* TooltablePy::templateAttrs(PyObject * args) { (void)args; PyObject *dict = PyDict_New(); - for(std::map::iterator i = getTooltablePtr()->Tools.begin(); i != getTooltablePtr()->Tools.end(); ++i) { + for(std::map::iterator i = getTooltablePtr()->Tools.begin(); i != getTooltablePtr()->Tools.end(); ++i) { // The 'tool' object must be created on the heap otherwise Python // will fail to properly track the reference counts and aborts // in debug mode.