Repeated BLE Begin/End crashes board

Hi all,

I'm new to using BLE on the Arduino platform. I was developing on the Nano 33 IoT a loop that toggles between gathering data on BLE and sending information on Wifi, and I realised that when I call the following loop, there appears to be a memory leak that causes the processor to crash after a few cycles.

... scan for 1min...

This happens when there is nothing else in the sketch. I'm suspecting that calling BLE.end() does not destroy all allocated resources. Does anyone else have a similar experience, and could anyone point me to the right direction on how to solve this?

I'm current using the following envrionment:

Board: Nano 33 IoT
ArduinoBLE: 1.2.1
Arduino IDE/Core: 1.8.15

Thanks so much for the help!

@jerteach @Klaus_K
Sorry for bothering you. I refer to your interesting exchange on:

Did you guys face the issue that the repeated BLE.begin() and BLE.end() eventually kills the board?


It looks like the issue comes from the following file:


In void GATTClass::begin() a couple of objects are created with new. They are not deleted in void GATTClass::end().

I changed that to

void GATTClass::end()
  delete( _genericAccessService );
  delete( _deviceNameCharacteristic );
  delete( _appearanceCharacteristic );
  delete( _genericAttributeService );
  delete( _servicesChangedCharacteristic );

I only used BLE.begin() and BLE.end() for the test and printed a counter value in loop. I run the test for a little while now and it seems fixed.

ArduinoBLE memory leak test (Click to open)
  This example tests the ArduinoBLE library for memory leaks.

  The circuit:
  - Arduino Nano 33 IoT

  This example code is in the public domain.

#include <ArduinoBLE.h>

void setup()
  Serial.begin( 9600 );
  while ( !Serial );
  Serial.println( "ArduinoBLE memory leak test" );

void loop()
  static uint32_t counter = 0;

  Serial.print( "C: " );
  Serial.println( counter );

I am not a C++ expert, so I am not sure if that is the right way to do this. This is just a lucky guess.

Can you confirm that this works for you? Then I can create an issue on GitHub so this gets fixed in the official library.

Edit: I opened an issue on GitHub

Hi @Klaus_K : This works well for at least a few hundred cycles, so thanks so much! Really appreciate the quick help!