diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2022-08-31 15:16:16 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-09-05 08:32:16 +0000 |
commit | bf595d0c6c83202713f954f0d97b08e3f4a6bbd6 (patch) | |
tree | ec978fd998778eac9488d7aa6f5fc5da3e8a8a86 | |
parent | f3588ec58d38283e908334e95480384e634ef026 (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.cpp | 9 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothdevicewatcher_winrt.cpp | 18 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothdevicewatcher_winrt_p.h | 1 |
3 files changed, 13 insertions, 15 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp index ada8b705..3a4552a4 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp @@ -153,19 +153,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 336e3cdc..455e6fc4 100644 --- a/src/bluetooth/qbluetoothdevicewatcher_winrt.cpp +++ b/src/bluetooth/qbluetoothdevicewatcher_winrt.cpp @@ -64,11 +64,7 @@ QBluetoothDeviceWatcherWinRT::QBluetoothDeviceWatcherWinRT(int id, winrt::hstrin QBluetoothDeviceWatcherWinRT::~QBluetoothDeviceWatcherWinRT() { - if (m_watcher && m_initialized) { - stop(); - unsubscribeFromEvents(); - m_initialized = false; - } + stop(); } bool QBluetoothDeviceWatcherWinRT::init() @@ -78,23 +74,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 603227d7..7435eeb7 100644 --- a/src/bluetooth/qbluetoothdevicewatcher_winrt_p.h +++ b/src/bluetooth/qbluetoothdevicewatcher_winrt_p.h @@ -91,7 +91,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; |