summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-12-13 17:29:04 +0100
committerIvan Solovev <ivan.solovev@qt.io>2021-12-14 11:42:53 +0100
commitd8473c74e16e2e1d99b97894616ca9a0cd0662c9 (patch)
tree95903951306377da2e16133e38000111f71a6200
parenta6f5b79169991ffde38643d7b1a04d6a9de137bf (diff)
downloadqtconnectivity-d8473c74e16e2e1d99b97894616ca9a0cd0662c9.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: Juha Vuolle <juha.vuolle@insta.fi>
-rw-r--r--src/bluetooth/qlowenergycontroller_winrt_new.cpp4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/bluetooth/qlowenergycontroller_winrt_new.cpp b/src/bluetooth/qlowenergycontroller_winrt_new.cpp
index e90073a4..b2ebab2b 100644
--- a/src/bluetooth/qlowenergycontroller_winrt_new.cpp
+++ b/src/bluetooth/qlowenergycontroller_winrt_new.cpp
@@ -404,6 +404,8 @@ public:
~QWinRTLowEnergyConnectionHandler()
{
qCDebug(QT_BT_WINRT) << __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.
@@ -436,7 +438,7 @@ private:
void QWinRTLowEnergyConnectionHandler::connectToDevice()
{
qCDebug(QT_BT_WINRT) << __FUNCTION__;
- mInitialized = CoInitialize(NULL);
+ mInitialized = CoInitializeEx(NULL, COINIT_MULTITHREADED);
qCDebug(QT_BT_WINRT) << qt_error_string(mInitialized);
auto earlyExit = [this]() { return mAbortConnection; };