diff --git a/src/Mod/Path/App/TooltablePyImp.cpp b/src/Mod/Path/App/TooltablePyImp.cpp index ef250a7bc1..63e4adfdaa 100644 --- a/src/Mod/Path/App/TooltablePyImp.cpp +++ b/src/Mod/Path/App/TooltablePyImp.cpp @@ -391,12 +391,18 @@ void TooltablePy::setTools(Py::Dict arg) } else { PyErr_Clear(); Path::Tool *tool = new Path::Tool; - Path::ToolPy pyTool(tool); - if (!pyTool.setFromTemplate(value)) { - PyErr_Print(); - throw Py::Exception("something went wrong"); + // The 'pyTool' object must be created on the heap otherwise Python + // will fail to properly track the reference counts and aborts + // in debug mode. + Path::ToolPy* pyTool = new Path::ToolPy(tool); + PyObject* success = pyTool->setFromTemplate(value); + if (!success) { + Py_DECREF(pyTool); + throw Py::Exception(); } getTooltablePtr()->setTool(*tool, ckey); + Py_DECREF(pyTool); + Py_DECREF(success); } } else { throw Py::Exception("The dictionary can only contain int:tool pairs");