From 738b904592f92c8ca675098bdf0c6720df9bdeac Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 11 Feb 2018 16:04:17 +0100 Subject: [PATCH] Fix invalid Python object error Example below led to a crash: import Path t = Path.Tool(name='t', diameter=1.2) tt = Path.Tooltable() tt.setTool(3, t) attrs = tt.templateAttrs() del attrs --- src/Mod/Path/App/TooltablePyImp.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Mod/Path/App/TooltablePyImp.cpp b/src/Mod/Path/App/TooltablePyImp.cpp index fd9f3b7a94..ef250a7bc1 100644 --- a/src/Mod/Path/App/TooltablePyImp.cpp +++ b/src/Mod/Path/App/TooltablePyImp.cpp @@ -515,9 +515,13 @@ PyObject* TooltablePy::templateAttrs(PyObject * args) (void)args; PyObject *dict = PyDict_New(); for(std::map::iterator i = getTooltablePtr()->Tools.begin(); i != getTooltablePtr()->Tools.end(); ++i) { - Path::ToolPy tool(new Path::Tool(*i->second)); - PyObject *attrs = tool.templateAttrs(0); + // The 'tool' object must be created on the heap otherwise Python + // will fail to properly track the reference counts and aborts + // in debug mode. + Path::ToolPy* tool = new Path::ToolPy(new Path::Tool(*i->second)); + PyObject *attrs = tool->templateAttrs(0); PyDict_SetItem(dict, PYINT_FROMLONG(i->first), attrs); + Py_DECREF(tool); } return dict; }