From 9eb078da5d84019ea9f6315620bc4a163a896572 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 4 Nov 2022 21:00:49 +0100 Subject: [PATCH] Gui: fix possible crash when running the garbage collector after creating a shiboken wrapper --- src/Gui/PythonWrapper.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Gui/PythonWrapper.cpp b/src/Gui/PythonWrapper.cpp index 807c8e1b57..0ee7d2b7fb 100644 --- a/src/Gui/PythonWrapper.cpp +++ b/src/Gui/PythonWrapper.cpp @@ -221,6 +221,8 @@ void registerTypes() // -------------------------------------------------------- namespace Gui { +template +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(); + if (type) { + auto sbk_type = reinterpret_cast(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; };