Fixed crash when reporting device lost

PiperOrigin-RevId: 522615946
This commit is contained in:
Guogang Li
2023-04-07 09:36:09 -07:00
committed by Copybara-Service
parent f0d95d9f58
commit 11158cb426
2 changed files with 12 additions and 0 deletions
@@ -17,6 +17,7 @@
#include <chrono> // NOLINT
#include <exception>
#include <future> // NOLINT
#include <list>
#include <memory>
#include <string>
#include <utility>
@@ -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.
@@ -18,6 +18,7 @@
#include <guiddef.h>
#include <future> // NOLINT
#include <list>
#include <memory>
#include <string>
#include <utility>
@@ -102,6 +103,12 @@ class BleMedium : public api::BleMedium {
absl::flat_hash_map<std::string, std::unique_ptr<BlePeripheral>>
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<std::unique_ptr<BlePeripheral>> lost_peripherals_
ABSL_GUARDED_BY(peripheral_map_mutex_);
// WinRT objects
::winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementPublisher publisher_ = nullptr;