From 3ca2a408fb1aa4da10df3ba4d1bd55c9e423ee8a Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 30 Mar 2018 18:39:55 +0200 Subject: [PATCH] Fix invalid Python object error Example below led to a crash: import Path v = Path.Tool(name='v', diameter=5.6) tt = Path.Tooltable() tt.addTools(v) attrs = tt.templateAttrs() uu = Path.Tooltable(tt.templateAttrs()) del attrs --- src/Mod/Path/App/TooltablePyImp.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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");