Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NimBLEScan::clearResults doesn't actually release memory space #73

Open
Zmmfly opened this issue Apr 28, 2022 · 1 comment
Open

NimBLEScan::clearResults doesn't actually release memory space #73

Zmmfly opened this issue Apr 28, 2022 · 1 comment

Comments

@Zmmfly
Copy link

Zmmfly commented Apr 28, 2022

Hello, I found that when there are too many Bluetooth devices around, the memory usage continues to rise after scanning is turned on.

I called "clearResults" to clean up, but it didn't work,
Then I read the code and found that "clear" is only called for "vector" in "clearResults", which doesn't actually free the memory occupied by the vector.

This is a "vector" based memory management problem.

void NimBLEScan::clearResults() {
for(auto &it: m_scanResults.m_advertisedDevicesVector) {
delete it;
}
m_scanResults.m_advertisedDevicesVector.clear();
clearDuplicateCache();
}

Add a line of code below "clear" to solve:
std::vector<NimBLEAdvertisedDevice *>().swap(m_scanResults.m_advertisedDevicesVector);

 void NimBLEScan::clearResults() { 
     for(auto &it: m_scanResults.m_advertisedDevicesVector) { 
         delete it; 
     } 
     m_scanResults.m_advertisedDevicesVector.clear(); 
     std::vector<NimBLEAdvertisedDevice *>().swap(m_scanResults.m_advertisedDevicesVector);
     clearDuplicateCache(); 
 } 
@h2zero
Copy link
Owner

h2zero commented Apr 28, 2022

I didn't think this would be too much of an issue since the vector would just allocate up to the peak number of devices found and not need to allocate again on the next scan. It's only around 250 bytes for 50 devices, but I will add this anyway.

If you'd like to PR the change feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants