Skip to content

Commit a08c2d0

Browse files
msmolensjcfr
andcommitted
Prevent crashes during and after cleanup
This commit prevents crashes by handling scenarios such as: (a) object destruction after the Python interpreter has been finalized (b) object destruction after cleanup, i.e. the singleton no longer exists Any usage of a Qt enum demonstrates (a). One example that demonstrates (b) is a QTimer object which is created with a QApplication parent. PythonQt::cleanup() is called before the QApplication is completely destroyed, so the code that handles wrapping the QTimer object must handle the case when the PythonQt singleton no longer exists. Co-authored-by: Jean-Christophe Fillion-Robin <[email protected]>
1 parent 0562458 commit a08c2d0

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

src/PythonQt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ class PYTHONQT_EXPORT PythonQt : public QObject {
526526
//@{
527527

528528
//! get access to internal data (should not be used on the public API, but is used by some C functions)
529-
static PythonQtPrivate* priv() { return _self->_p; }
529+
static PythonQtPrivate* priv() { return _self ? _self->_p : NULL; }
530530

531531
//! clear all NotFound entries on all class infos, to ensure that
532532
//! newly loaded wrappers can add methods even when the object was wrapped by PythonQt before the wrapper was loaded

src/PythonQtObjectPtr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ PythonQtObjectPtr::PythonQtObjectPtr(PyObject* o)
4949

5050
PythonQtObjectPtr::~PythonQtObjectPtr()
5151
{
52-
if (_object) Py_DECREF(_object);
52+
if (_object && Py_IsInitialized()) Py_DECREF(_object);
5353
}
5454

5555
void PythonQtObjectPtr::setNewRef(PyObject* o)

src/PythonQtSignalReceiver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ PythonQtSignalReceiver::PythonQtSignalReceiver(QObject* obj):PythonQtSignalRecei
177177

178178
PythonQtSignalReceiver::~PythonQtSignalReceiver()
179179
{
180-
PythonQt::priv()->removeSignalEmitter(_obj);
180+
if (PythonQt::priv()) {
181+
PythonQt::priv()->removeSignalEmitter(_obj);
182+
}
181183
}
182184

183185

0 commit comments

Comments
 (0)