summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2022-08-31 15:16:16 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-09-01 15:34:29 +0000
commite8240ffe2adcbc57b725dc3d726317451a7e9324 (patch)
treeab988299f19f8e7bf2fcc19b843e1a7a943ed779
parent2e8c556cac736de383cc27fb8171e0387f24541c (diff)
Windows device discovery: fix memory leaks
Both QBluetoothDeviceWatcherWinRT and AdvertisementWatcherWrapper helper classes have the same issue. They pass a shared_from_this() pointer to the event-handling lambdas to make sure that the object is alive when the event is received. Both classes have unsubscribeFromEvents() private methods, which are used to unsibscribe from windows events, and so release the previously captured shared_from_this() instances. However the initial implementation was trying to call these methods only in destructors. And as a result they never got called, because the captured shared_from_this() pointers prevented the destruction of the objects. This patch moves the unsubscribeFromEvents() calls to the stop() function, which is invoked from the clien code when we stop handling the events. This implies that we need to move the call to subscribeToEvents() function inside start(). In case of QBluetoothDeviceWatcherWinRT this also allows us to simplify the code by getting rid of now redundant m_initialized flag. This commit amends 761a059d5a5ef97e97039f3a34b3a7f92944f1f0 and 36dd802c964f97522d1f5a75c8fb7a67f3061a3d. Fixes: QTBUG-105742 Change-Id: I46f263750a26cc4265081db325302123c7548a1c Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi> Reviewed-by: Alex Blasche <alexander.blasche@qt.io> (cherry picked from commit 37a9d533c541db63fd1b7c261ca832ea56ab8203) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp9
-rw-r--r--src/bluetooth/qbluetoothdevicewatcher_winrt.cpp18
-rw-r--r--src/bluetooth/qbluetoothdevicewatcher_winrt_p.h1
3 files changed, 13 insertions, 15 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp
index 7fceb041..37659d4b 100644
--- a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp
+++ b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp
@@ -120,19 +120,22 @@ public:
AdvertisementWatcherWrapper() {}
~AdvertisementWatcherWrapper()
{
- unsubscribeFromEvents();
stop();
}
void init()
{
m_watcher.ScanningMode(BluetoothLEScanningMode::Active);
+ }
+ void start() {
subscribeToEvents();
+ m_watcher.Start();
}
- void start() { m_watcher.Start(); }
void stop()
{
- if (canStop())
+ if (canStop()) {
+ unsubscribeFromEvents();
m_watcher.Stop();
+ }
}
signals:
diff --git a/src/bluetooth/qbluetoothdevicewatcher_winrt.cpp b/src/bluetooth/qbluetoothdevicewatcher_winrt.cpp
index a61cde87..fd69ee77 100644
--- a/src/bluetooth/qbluetoothdevicewatcher_winrt.cpp
+++ b/src/bluetooth/qbluetoothdevicewatcher_winrt.cpp
@@ -28,11 +28,7 @@ QBluetoothDeviceWatcherWinRT::QBluetoothDeviceWatcherWinRT(int id, winrt::hstrin
QBluetoothDeviceWatcherWinRT::~QBluetoothDeviceWatcherWinRT()
{
- if (m_watcher && m_initialized) {
- stop();
- unsubscribeFromEvents();
- m_initialized = false;
- }
+ stop();
}
bool QBluetoothDeviceWatcherWinRT::init()
@@ -42,23 +38,23 @@ bool QBluetoothDeviceWatcherWinRT::init()
"Detection of Bluetooth devices might not work correctly.");
return false;
}
- if (!m_initialized) {
- subscribeToEvents();
- m_initialized = true;
- }
return true;
}
void QBluetoothDeviceWatcherWinRT::start()
{
- if (m_watcher)
+ if (m_watcher) {
+ subscribeToEvents();
m_watcher.Start();
+ }
}
void QBluetoothDeviceWatcherWinRT::stop()
{
- if (m_watcher && canStop())
+ if (m_watcher && canStop()) {
+ unsubscribeFromEvents();
m_watcher.Stop();
+ }
}
void QBluetoothDeviceWatcherWinRT::subscribeToEvents()
diff --git a/src/bluetooth/qbluetoothdevicewatcher_winrt_p.h b/src/bluetooth/qbluetoothdevicewatcher_winrt_p.h
index c0551036..08ab0c5e 100644
--- a/src/bluetooth/qbluetoothdevicewatcher_winrt_p.h
+++ b/src/bluetooth/qbluetoothdevicewatcher_winrt_p.h
@@ -55,7 +55,6 @@ private:
const int m_id; // used to uniquely identify the wrapper
winrt::Windows::Devices::Enumeration::DeviceWatcher m_watcher = nullptr;
- bool m_initialized = false;
winrt::event_token m_addedToken;
winrt::event_token m_removedToken;