Gui: fix possible crash when running the garbage collector after creating a shiboken wrapper

This commit is contained in:
wmayer
2022-11-04 21:00:49 +01:00
parent 192666d1ac
commit 9eb078da5d

View File

@@ -221,6 +221,8 @@ void registerTypes()
// --------------------------------------------------------
namespace Gui {
template<typename qttype>
PyTypeObject *getPyTypeObjectForTypeName();
/*!
* \brief The WrapperManager class
@@ -292,11 +294,29 @@ private:
Base::PyGILStateLocker lock;
wrappers.clear();
}
void wrapQApplication()
{
// We have to explicitly hold a reference to the wrapper of the QApplication
// as otherwise it can happen that when running the gc the program crashes
// The code snippet below caused a crash on older versions:
// mw = Gui.getMainWindow()
// mw.style()
// import gc
// gc.collect()
PyTypeObject * type = getPyTypeObjectForTypeName<QApplication>();
if (type) {
auto sbk_type = reinterpret_cast<SbkObjectType*>(type);
std::string typeName = "QApplication";
PyObject* pyobj = Shiboken::Object::newObject(sbk_type, qApp, false, false, typeName.c_str());
addQObject(qApp, pyobj);
}
}
WrapperManager()
{
connect(QApplication::instance(), &QCoreApplication::aboutToQuit,
this, &WrapperManager::clear);
wrapQApplication();
}
~WrapperManager() = default;
};