Closed
Description
The issue was reported by a user in the Arduino forum, who wants to switch between BLE and WiFi on a regular basis.
https://forum.arduino.cc/t/repeated-ble-begin-end-crashes-board/885009
I wrote the following sketch for testing and it crashes after 50 loops.
#include <ArduinoBLE.h>
void setup()
{
Serial.begin( 9600 );
while ( !Serial );
Serial.println( "ArduinoBLE memory leak test" );
}
void loop()
{
static uint32_t counter = 0;
BLE.begin();
counter++;
Serial.print( "C: " );
Serial.println( counter );
BLE.end();
}
With my limited C++ knowledge I found in void GATTClass::begin() a couple of objects are created with new. They are not deleted in void GATTClass::end().
Adding the 5 deletes to GATTClass::end seems to fix the issue. Can you confirm this is the right way to fix this and implement this in the next version of the library?
void GATTClass::end()
{
delete( _genericAccessService );
delete( _deviceNameCharacteristic );
delete( _appearanceCharacteristic );
delete( _genericAttributeService );
delete( _servicesChangedCharacteristic );
_attributes.clear();
}