diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-12-06 18:16:15 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-12-13 14:51:22 +0000 |
commit | 95aea43ff693584618f0dfa3f850c3fc568ee705 (patch) | |
tree | 5f32fbd175f5474695e41db82bca026eec2d7971 | |
parent | 19d80d16d7f7b9d605c024dc8650a4f541e51e03 (diff) | |
download | qtconnectivity-95aea43ff693584618f0dfa3f850c3fc568ee705.tar.gz |
QLowEnergyController Windows: fix early disconnect crash on Win 11
QLowEnergyController connection helper object was calling CoInitialize()
in connectToDevice() and CoUninitialize() in its destructor.
This was working fine on Windows 10, but on Windows 11, in case when
the connection attempt is interrupted by a disconnect request, it was
crashing.
The stack trace showed that Windows 11 is trying to do more COM-related
calls after CoUninitialize() is called. Specifically, it was calling
CoIncrementMTAUsage(), and some others.
This patch replaces CoInitialize(), which uses a single-threaded
apartment model, to CoInitializeEx(NULL, COINIT_MULTITHREADED) that
uses multi-threaded apartment. It also explicitly clears the
ComPtr-based class members before calling CoUninitialize().
These two measures help to prevent the crashes. Although I do not
completely understand why it helps.
Fixes: QTBUG-98582
Change-Id: I6fe5a877dbc2c0518ba97fddb132c2f87fc397c2
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
(cherry picked from commit 6b910367d859504a0bb48b82e6a2400f76e35241)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/bluetooth/qlowenergycontroller_winrt.cpp | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/src/bluetooth/qlowenergycontroller_winrt.cpp b/src/bluetooth/qlowenergycontroller_winrt.cpp index 69e8dd38..b33d67f9 100644 --- a/src/bluetooth/qlowenergycontroller_winrt.cpp +++ b/src/bluetooth/qlowenergycontroller_winrt.cpp @@ -392,6 +392,8 @@ public: ~QWinRTLowEnergyConnectionHandler() { qCDebug(QT_BT_WINDOWS) << __FUNCTION__; + mDevice.Reset(); + mGattSession.Reset(); // To close the COM library gracefully, each successful call to // CoInitialize, including those that return S_FALSE, must be balanced // by a corresponding call to CoUninitialize. @@ -424,7 +426,7 @@ private: void QWinRTLowEnergyConnectionHandler::connectToDevice() { qCDebug(QT_BT_WINDOWS) << __FUNCTION__; - mInitialized = CoInitialize(NULL); + mInitialized = CoInitializeEx(NULL, COINIT_MULTITHREADED); qCDebug(QT_BT_WINDOWS) << qt_error_string(mInitialized); auto earlyExit = [this]() { return mAbortConnection; }; |