mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2025-01-15 22:12:08 +01:00
Fix race condition in NimBLEScan::clearResults.
If clear results is called from more than one task a race condition exists that may delete the same advertisedDevice twice. This prevents this by swapping with an empty vector and testing for empty before freeing the resources.
This commit is contained in:
parent
a9578637cb
commit
e6249623d5
1 changed files with 8 additions and 3 deletions
|
@ -468,10 +468,15 @@ NimBLEScanResults NimBLEScan::getResults() {
|
||||||
* @brief Clear the stored results of the scan.
|
* @brief Clear the stored results of the scan.
|
||||||
*/
|
*/
|
||||||
void NimBLEScan::clearResults() {
|
void NimBLEScan::clearResults() {
|
||||||
for (const auto& dev : m_scanResults.m_deviceVec) {
|
if (m_scanResults.m_deviceVec.size()) {
|
||||||
delete dev;
|
std::vector<NimBLEAdvertisedDevice*> vSwap{};
|
||||||
|
ble_npl_hw_enter_critical();
|
||||||
|
vSwap.swap(m_scanResults.m_deviceVec);
|
||||||
|
ble_npl_hw_exit_critical(0);
|
||||||
|
for (const auto& dev : vSwap) {
|
||||||
|
delete dev;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
std::vector<NimBLEAdvertisedDevice*>().swap(m_scanResults.m_deviceVec);
|
|
||||||
} // clearResults
|
} // clearResults
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue