Skip to content

Commit 4c8d5c1

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 6ce9420 commit 4c8d5c1

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

src/PythonQt.h

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

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

557557
//! clear all NotFound entries on all class infos, to ensure that
558558
//! 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
@@ -99,7 +99,7 @@ PythonQtObjectPtr::PythonQtObjectPtr(PythonQtSafeObjectPtr &&p) :_object(p.takeO
9999

100100
PythonQtObjectPtr::~PythonQtObjectPtr()
101101
{
102-
if (_object) Py_DECREF(_object);
102+
if (_object && Py_IsInitialized()) Py_DECREF(_object);
103103
}
104104

105105
void PythonQtObjectPtr::setNewRef(PyObject* o)

src/PythonQtSignalReceiver.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,12 @@ PythonQtSignalReceiver::PythonQtSignalReceiver(QObject* obj):PythonQtSignalRecei
178178

179179
PythonQtSignalReceiver::~PythonQtSignalReceiver()
180180
{
181-
// we need the GIL scope here, because the targets keep references to Python objects
182-
PYTHONQT_GIL_SCOPE;
183-
PythonQt::priv()->removeSignalEmitter(_obj);
184-
_targets.clear();
181+
if (PythonQt::priv()) {
182+
// we need the GIL scope here, because the targets keep references to Python objects
183+
PYTHONQT_GIL_SCOPE;
184+
PythonQt::priv()->removeSignalEmitter(_obj);
185+
_targets.clear();
186+
}
185187
}
186188

187189

0 commit comments

Comments
 (0)