-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
BLE library has a memory leak. #4753
Comments
Your memory leak is because you create a new BLEClient instance every time |
BLEClient should be deleted by library after disconnecting, but i agree, try to use NimBLE, more pros than cons. |
Thanks for the reply. I fixed it to delete the client at the end of connectToServer, but now the device crashes. bool connectToServer()
{
BLEClient *pClient = BLEDevice::createClient();
if (!pClient->connect(myDevice))
{
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
return false;
}
BLERemoteService *pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr)
{
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
BLERemoteCharacteristic *pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr)
{
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
pClient->disconnect();
return false;
}
if (pRemoteCharacteristic->canRead())
{
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
pClient->disconnect();
delete pClient; // added
return true;
} Here is the log.
I'll refer to NimBLE. |
Oops, I forgot about this, sorry for bad info. Curious about the memory leak then. |
Good approach, definitely looks like there's some bugs to stomp. |
The crashes that sometimes occurred with the above approach seem to have gone away after the following modification to the BLEClient destructor. BLEClient::~BLEClient() {
// We may have allocated service references associated with this client. Before we are finished
// with the client, we must release resources.
for (auto &myPair : m_servicesMap) {
delete myPair.second;
}
m_servicesMap.clear();
m_servicesMapByInstID.clear(); // added
} // ~BLEClient But, the free space in the heap memory is still decreasing. |
just a side note: it is perfectly legal to call free() or delete() on a nullptr, so any check that only guards one of the two calls is unnecessary at best or confusing otherwise. |
Thanks for your advice. |
* Fixed crash on delete after disconnect * Fixed memory leak when getting characteristics * Removed guard Co-authored-by: ushiboy <[email protected]>
Hardware:
Board: ESP32 Dev Module
Core Installation version: 1.0.5-rc6
IDE name: Arduino IDE 1.8.13
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 921600
Computer OS: Ubuntu 18.04.5 LTS
Description:
I ran the following code as a central device for BLE.
Then the free space in the heap memory gradually decreased and finally the device hung.
I used the BLE_server sample sketch for the peripherals.
Sketch:
Debug Messages:
The text was updated successfully, but these errors were encountered: