diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 1836405b9c..676a529dfe 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -4913,6 +4913,16 @@ QMetaObject::Connection::~Connection() static_cast(d_ptr)->deref(); } +/*! \internal Returns true if the object is still connected */ +bool QMetaObject::Connection::isConnected_helper() const +{ + Q_ASSERT(d_ptr); // we're only called from operator RestrictedBool() const + QObjectPrivate::Connection *c = static_cast(d_ptr); + + return c->receiver; +} + + /*! \fn QMetaObject::Connection::operator bool() const diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h index 4d01264906..6484507a12 100644 --- a/src/corelib/kernel/qobjectdefs.h +++ b/src/corelib/kernel/qobjectdefs.h @@ -472,6 +472,7 @@ class Q_CORE_EXPORT QMetaObject::Connection { friend class QObject; friend class QObjectPrivate; friend struct QMetaObject; + bool isConnected_helper() const; public: ~Connection(); Connection(); @@ -481,7 +482,7 @@ public: operator bool() const; #else typedef void *Connection::*RestrictedBool; - operator RestrictedBool() const { return d_ptr ? &Connection::d_ptr : 0; } + operator RestrictedBool() const { return d_ptr && isConnected_helper() ? &Connection::d_ptr : 0; } #endif #ifdef Q_COMPILER_RVALUE_REFS diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 3ec84b5198..4617ce5e74 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -918,6 +918,8 @@ void tst_QObject::connectDisconnectNotifyPMF() QMetaObject::Connection conn = connect((SenderObject*)s, &SenderObject::signal1, (ReceiverObject*)r, &ReceiverObject::slot1); + QVERIFY(conn); + // Test disconnectNotify when disconnecting by QMetaObject::Connection QVERIFY(QObject::disconnect(conn)); // disconnectNotify() is not called, but it probably should be. @@ -5751,7 +5753,6 @@ void tst_QObject::connectFunctorWithContext() { int status = 1; SenderObject obj; - QMetaObject::Connection handle; ContextObject *context = new ContextObject; QEventLoop e; @@ -6058,8 +6059,12 @@ void tst_QObject::disconnectDoesNotLeakFunctor() QVERIFY(c2); QCOMPARE(countedStructObjectsCount, 2); QVERIFY(QObject::disconnect(c1)); + QVERIFY(!c1); + QVERIFY(!c2); // functor object has been destroyed QCOMPARE(countedStructObjectsCount, 1); + QVERIFY(!QObject::disconnect(c2)); + QCOMPARE(countedStructObjectsCount, 1); } QCOMPARE(countedStructObjectsCount, 0); }