From 5e66767fca9908c397705ed557b106ec2c3a63f8 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 6 Jul 2018 13:37:44 -0700 Subject: [PATCH] Make sure there's an event dispatcher in QWindowsRemovableDriveListener In order to add the listener, we need to hook into the internal HWND. Creating the QThreadData (thus adopting the thread) and the event dispatcher are ok because they would happen anyway. QFileSystemWatcher works only with the Qt event loop. [ChangeLog][QtCore][QFileSystemWatcher] Fixed a bug that caused addPaths() to crash on Windows if adding a path to be watched on removeable storage, if that addPaths() was called on a thread not created by QThread and no QEventLoop has been created yet. Task-number: QTBUG-69320 Change-Id: If48c5c2e920c433298f1fffd153ee20bea56c62f Reviewed-by: Friedemann Kleint --- src/corelib/io/qfilesystemwatcher_win.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/corelib/io/qfilesystemwatcher_win.cpp b/src/corelib/io/qfilesystemwatcher_win.cpp index 9e43d11e71..3a49f4346b 100644 --- a/src/corelib/io/qfilesystemwatcher_win.cpp +++ b/src/corelib/io/qfilesystemwatcher_win.cpp @@ -57,6 +57,7 @@ # include # include # include +# include # include # include # include @@ -308,7 +309,8 @@ void QWindowsRemovableDriveListener::addPath(const QString &p) notify.dbch_size = sizeof(notify); notify.dbch_devicetype = DBT_DEVTYP_HANDLE; notify.dbch_handle = volumeHandle; - QEventDispatcherWin32 *winEventDispatcher = static_cast(QAbstractEventDispatcher::instance()); + QThreadData *currentData = QThreadData::current(); + QEventDispatcherWin32 *winEventDispatcher = static_cast(currentData->ensureEventDispatcher()); re.devNotify = RegisterDeviceNotification(winEventDispatcher->internalHwnd(), ¬ify, DEVICE_NOTIFY_WINDOW_HANDLE); // Empirically found: The notifications also work when the handle is immediately