Path: [skip ci] fix memory leaks

This commit is contained in:
wmayer
2021-04-27 14:32:35 +02:00
parent 1e5c5f4e7f
commit 346fe8a93a
4 changed files with 14 additions and 11 deletions

View File

@@ -23,6 +23,7 @@
#ifndef PATH_TOOL_H
#define PATH_TOOL_H
#include <memory>
#include <vector>
#include <string>
#include <map>
@@ -99,6 +100,8 @@ namespace Path
static ToolMaterial getToolMaterial(std::string mat);
static const char* MaterialName(ToolMaterial mat);
};
using ToolPtr = std::shared_ptr<Tool>;
} //namespace Path
#endif // PATH_TOOL_H

View File

@@ -47,10 +47,10 @@ Tooltable::~Tooltable()
void Tooltable::addTool(const Tool &tool)
{
Tool *tmp = new Tool(tool);
ToolPtr tmp = std::make_shared<Tool>(tool);
if (!Tools.empty()) {
int max = 0;
for(std::map<int,Tool*>::const_iterator i = Tools.begin(); i != Tools.end(); ++i) {
for(std::map<int,ToolPtr>::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>(tool);
Tools[pos] = tmp;
}
}
@@ -88,9 +88,9 @@ void Tooltable::Save (Writer &writer) const
{
writer.Stream() << writer.ind() << "<Tooltable count=\"" << getSize() <<"\">" << std::endl;
writer.incInd();
for(std::map<int,Tool*>::const_iterator i = Tools.begin(); i != Tools.end(); ++i) {
for(std::map<int,ToolPtr>::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() << "<Toolslot number=\"" << k << "\">" << 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<Tool>();
tmp->Restore(reader);
Tools[id] = tmp;
}

View File

@@ -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<int,Tool*> &getTools(void) const {return Tools;}
const Tool &getTool(int pos) {return *Tools.at(pos);}
const std::map<int,ToolPtr> &getTools(void) const {return Tools;}
bool hasTool(int pos) const {return (Tools.count(pos) != 0);}
// attributes
std::map<int,Tool*> Tools;
std::map<int,ToolPtr> Tools;
int Version;
std::string Name;
};

View File

@@ -97,7 +97,7 @@ int TooltablePy::PyInit(PyObject* args, PyObject* /*kwd*/)
Py::Dict TooltablePy::getTools(void) const
{
Py::Dict dict;
for(std::map<int,Path::Tool*>::iterator i = getTooltablePtr()->Tools.begin(); i != getTooltablePtr()->Tools.end(); ++i) {
for(std::map<int,Path::ToolPtr>::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<int,Path::Tool*>::iterator i = getTooltablePtr()->Tools.begin(); i != getTooltablePtr()->Tools.end(); ++i) {
for(std::map<int,Path::ToolPtr>::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.