Skip to content

Commit cb0cee9

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 b0194d0 commit cb0cee9

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

src/PythonQt.h

+1-1
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

+1-1
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ PythonQtSignalReceiver::PythonQtSignalReceiver(QObject* obj):PythonQtSignalRecei
172172

173173
PythonQtSignalReceiver::~PythonQtSignalReceiver()
174174
{
175-
PythonQt::priv()->removeSignalEmitter(_obj);
175+
if (PythonQt::priv()) {
176+
PythonQt::priv()->removeSignalEmitter(_obj);
177+
}
176178
}
177179

178180

0 commit comments

Comments
 (0)