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
Arduino IDE/Core: 1.8.15
Thanks so much for the help!
Sorry for bothering you. I refer to your interesting exchange on:
Can someone tell me what I'm doing wrong ?
Please have a look at the example in reply #6 in
Switching between Wifi and BLE - Nano 33 IoT - Arduino Forum.
You should not have any WiFi or BLE code in setup(). setup() is only run once when you power up or reset the board. When you switch you need to stop BLE or WiFi and then start BLE or WiFi.
The example shows you exactly what you need to do. Try to understand it first. You removed important functionality from the functions I wrot…
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
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.
- Arduino Nano 33 IoT
This example code is in the public domain.
Serial.begin( 9600 );
while ( !Serial );
Serial.println( "ArduinoBLE memory leak test" );
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
@Klaus_K : This works well for at least a few hundred cycles, so thanks so much! Really appreciate the quick help!
November 11, 2021, 6:18am
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.