diff --git a/internal/platform/implementation/windows/ble_medium.cc b/internal/platform/implementation/windows/ble_medium.cc index 9af49ba4..96eb574e 100644 --- a/internal/platform/implementation/windows/ble_medium.cc +++ b/internal/platform/implementation/windows/ble_medium.cc @@ -17,6 +17,7 @@ #include // NOLINT #include #include // NOLINT +#include #include #include #include @@ -287,6 +288,7 @@ bool BleMedium::StartScanning( { absl::MutexLock lock(&peripheral_map_mutex_); peripheral_map_.clear(); + lost_peripherals_.clear(); } watcher_ = BluetoothLEAdvertisementWatcher(); @@ -596,6 +598,9 @@ void BleMedium::AdvertisementReceivedHandler( /*ble_peripheral*/ *peripheral_map_[peripheral_name], /*service_id*/ service_id_); + // put the lost peripheral in the lost peripheral list. + lost_peripherals_.push_back( + std::move(peripheral_map_[peripheral_name])); } else { // The device is already reported to discover, so don't need to // call it again. diff --git a/internal/platform/implementation/windows/ble_medium.h b/internal/platform/implementation/windows/ble_medium.h index 9a34cc85..b25ebc53 100644 --- a/internal/platform/implementation/windows/ble_medium.h +++ b/internal/platform/implementation/windows/ble_medium.h @@ -18,6 +18,7 @@ #include #include // NOLINT +#include #include #include #include @@ -102,6 +103,12 @@ class BleMedium : public api::BleMedium { absl::flat_hash_map> peripheral_map_ ABSL_GUARDED_BY(peripheral_map_mutex_); + // The platform implementation will reference lost peripheral in another + // thread after report loss, so we still need to keep the peripheral to + // avoid potential memory issues. + std::list> lost_peripherals_ + ABSL_GUARDED_BY(peripheral_map_mutex_); + // WinRT objects ::winrt::Windows::Devices::Bluetooth::Advertisement:: BluetoothLEAdvertisementPublisher publisher_ = nullptr;