diff --git a/src/network/kernel/qhostinfo.cpp b/src/network/kernel/qhostinfo.cpp index 6662ae1eee..dab4d055ed 100644 --- a/src/network/kernel/qhostinfo.cpp +++ b/src/network/kernel/qhostinfo.cpp @@ -73,10 +73,10 @@ Q_APPLICATION_STATIC(QHostInfoLookupManager, theHostInfoLookupManager) } QHostInfoResult::QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot) - : receiver{receiver}, slotObj{std::move(slot)}, withContextObject{slotObj && receiver} + : receiver{receiver ? receiver : this}, slotObj{std::move(slot)} { - if (receiver) - moveToThread(receiver->thread()); + Q_ASSERT(this->receiver); + moveToThread(this->receiver->thread()); } QHostInfoResult::~QHostInfoResult() @@ -101,7 +101,7 @@ void QHostInfoResult::postResultsReady(const QHostInfo &info) return; } // we used to have a context object, but it's already destroyed - if (withContextObject && !receiver) + if (!receiver) return; static const int signal_index = []() -> int { @@ -129,11 +129,13 @@ bool QHostInfoResult::event(QEvent *event) { if (event->type() == QEvent::MetaCall) { Q_ASSERT(slotObj); - auto metaCallEvent = static_cast(event); - auto args = metaCallEvent->args(); - // we didn't have a context object, or it's still alive - if (!withContextObject || receiver) + + // we used to have a context object, but it's already destroyed + if (receiver) { + auto metaCallEvent = static_cast(event); + auto args = metaCallEvent->args(); slotObj->call(const_cast(receiver.data()), args); + } deleteLater(); return true; diff --git a/src/network/kernel/qhostinfo_p.h b/src/network/kernel/qhostinfo_p.h index 70cacc6b3d..4c0d5010fc 100644 --- a/src/network/kernel/qhostinfo_p.h +++ b/src/network/kernel/qhostinfo_p.h @@ -56,8 +56,8 @@ protected: private: QHostInfoResult(const QHostInfoResult *other) - : receiver(other->receiver), slotObj{copy(other->slotObj)}, - withContextObject(other->withContextObject) + : receiver(other->receiver.get() != other ? other->receiver.get() : this), + slotObj{copy(other->slotObj)} { // cleanup if the application terminates before results are delivered connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, @@ -66,9 +66,10 @@ private: moveToThread(other->thread()); } + // receiver is either a QObject provided by the user, + // or it's set to `this` (to emulate the behavior of the contextless connect()) QPointer receiver = nullptr; QtPrivate::SlotObjUniquePtr slotObj; - const bool withContextObject = false; }; class QHostInfoAgent