python封装类,debug版本不清理,否则崩溃

This commit is contained in:
DESKTOP-450PEFP\mainc 2026-04-10 10:25:34 +08:00
parent 5d054c35db
commit 058bd7e337

View File

@ -199,7 +199,10 @@ QVariant PythonHandler::pythonObjectToQVariant(PyObject *obj)
map[key] = pythonObjectToQVariant(pValue); map[key] = pythonObjectToQVariant(pValue);
} }
} }
#ifdef _RELEASE
Py_DECREF(pKeys); Py_DECREF(pKeys);
#endif
return map; return map;
} }
else if (obj == Py_None) { else if (obj == Py_None) {
@ -222,14 +225,17 @@ QString PythonHandler::capturePythonError() {
PyObject* pstr = PyObject_Str(pvalue); PyObject* pstr = PyObject_Str(pvalue);
if (pstr) { if (pstr) {
errorMsg = QString::fromUtf8(PyUnicode_AsUTF8(pstr)); errorMsg = QString::fromUtf8(PyUnicode_AsUTF8(pstr));
#ifdef _RELEASE
Py_DECREF(pstr); Py_DECREF(pstr);
#endif
} }
} }
#ifdef _RELEASE
// 清理 // 清理
Py_XDECREF(ptype); Py_XDECREF(ptype);
Py_XDECREF(pvalue); Py_XDECREF(pvalue);
Py_XDECREF(ptraceback); Py_XDECREF(ptraceback);
#endif
return errorMsg; return errorMsg;
} }
@ -284,8 +290,11 @@ QVariant PythonHandler::executeScript(const QString &scriptPath,
PyErr_Print(); PyErr_Print();
} }
emit addLog("无法找到函数:" + functionName); emit addLog("无法找到函数:" + functionName);
#ifdef _RELEASE
Py_XDECREF(pFunc); Py_XDECREF(pFunc);
Py_DECREF(pModule); Py_DECREF(pModule);
#endif
return QVariant(); return QVariant();
} }
@ -427,7 +436,10 @@ QVariant PythonHandler::executeCode(const QString &code)
if (pResult) { if (pResult) {
QVariant result = pythonObjectToQVariant(pResult); QVariant result = pythonObjectToQVariant(pResult);
#ifdef _RELEASE
Py_DECREF(pResult); Py_DECREF(pResult);
#endif
return result; return result;
} else { } else {
PyErr_Print(); PyErr_Print();