From 7b7ad02681f2c28fb18d053618f6804e989b2f56 Mon Sep 17 00:00:00 2001 From: Jonathan Liu Date: Thu, 18 Sep 2014 19:54:46 +1000 Subject: Improve checking for event/socket notifiers and timers Starting/stopping timers from another thread may result in errors that may not appear until hours, days or weeks after if a release build of Qt is used with the GLib/UNIX event dispatchers. Such errors may manifest as warnings such as "QObject::killTimer(): Error: timer id 7 is not valid for object 0x2a51b488 (), timer has not been killed" and application crashes (e.g. crashes in malloc, realloc and malloc_consolidate). Initial-patch-by: Eike Ziller Task-number: QTBUG-40636 Change-Id: I2de50d50eb1fc7467fcebb9c73b74d2f85137933 Reviewed-by: Jocelyn Turcotte Reviewed-by: Thiago Macieira --- src/corelib/kernel/qwineventnotifier.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/corelib/kernel/qwineventnotifier.cpp') diff --git a/src/corelib/kernel/qwineventnotifier.cpp b/src/corelib/kernel/qwineventnotifier.cpp index c694237dc3..d2c623d489 100644 --- a/src/corelib/kernel/qwineventnotifier.cpp +++ b/src/corelib/kernel/qwineventnotifier.cpp @@ -140,11 +140,11 @@ QWinEventNotifier::QWinEventNotifier(HANDLE hEvent, QObject *parent) { Q_D(QWinEventNotifier); QAbstractEventDispatcher *eventDispatcher = d->threadData->eventDispatcher.load(); - if (!eventDispatcher) { + if (Q_UNLIKELY(!eventDispatcher)) { qWarning("QWinEventNotifier: Can only be used with threads started with QThread"); - } else { - eventDispatcher->registerEventNotifier(this); + return; } + eventDispatcher->registerEventNotifier(this); d->enabled = true; } @@ -215,6 +215,10 @@ void QWinEventNotifier::setEnabled(bool enable) QAbstractEventDispatcher *eventDispatcher = d->threadData->eventDispatcher.load(); if (!eventDispatcher) // perhaps application is shutting down return; + if (Q_UNLIKELY(thread() != QThread::currentThread())) { + qWarning("QWinEventNotifier: Event notifiers cannot be enabled or disabled from another thread"); + return; + } if (enable) eventDispatcher->registerEventNotifier(this); -- cgit v1.2.3