diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index e53054b6d7..8a31633f38 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -1415,8 +1415,11 @@ bool QObject::event(QEvent *e) if (eventDispatcher) { QList timers = eventDispatcher->registeredTimers(this); if (!timers.isEmpty()) { + const bool res = eventDispatcher->unregisterTimers(this); // do not to release our timer ids back to the pool (since the timer ids are moving to a new thread). - eventDispatcher->unregisterTimers(this); + Q_ASSERT_X(res, Q_FUNC_INFO, + "QAbstractEventDispatcher::unregisterTimers() returned false," + " but there are timers associated with this object."); QMetaObject::invokeMethod(this, "_q_reregisterTimers", Qt::QueuedConnection, Q_ARG(void*, (new QList(timers)))); } diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp index bcec650987..3a823cf53e 100644 --- a/src/corelib/kernel/qtimerinfo_unix.cpp +++ b/src/corelib/kernel/qtimerinfo_unix.cpp @@ -353,21 +353,23 @@ bool QTimerInfoList::unregisterTimers(QObject *object) { if (isEmpty()) return false; - for (int i = 0; i < size(); ++i) { - QTimerInfo *t = at(i); - if (t->obj == object) { - // object found - removeAt(i); - if (t == firstTimerInfo) - firstTimerInfo = nullptr; - if (t->activateRef) - *(t->activateRef) = nullptr; - delete t; - // move back one so that we don't skip the new current item - --i; - } - } - return true; + + auto associatedWith = [this](QObject *o) { + return [this, o](auto &t) { + if (t->obj == o) { + if (t == firstTimerInfo) + firstTimerInfo = nullptr; + if (t->activateRef) + *(t->activateRef) = nullptr; + delete t; + return true; + } + return false; + }; + }; + + qsizetype count = removeIf(associatedWith(object)); + return count > 0; } QList QTimerInfoList::registeredTimers(QObject *object) const