Skip to content

BLEServer Memory Leak after device connecting and disconnecting #10130

Closed
@victorfleischauer

Description

@victorfleischauer

Board

ESP32-S3-DevKitC-1

Device Description

ESP32-S3-DevKitC-1 v1.0

Hardware Configuration

No connections.

Version

v3.0.4

IDE Name

PlatformIO

Operating System

macOS 14.5

Flash frequency

40Mhz

PSRAM enabled

no

Upload speed

460800

Description

When acting as a BLEServer, after every connection and disconnection the heap memory appears to shrink by
around 412 bytes (except the first connection, which loses around 1 kilobyte)
This appears to be a memory leak.

Sketch

#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;

bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks : public BLEServerCallbacks
{
  void onConnect(BLEServer *pServer)
  {
    deviceConnected = true;
  };

  void onDisconnect(BLEServer *pServer)
  {
    deviceConnected = false;
  }
};

void setup()
{
  Serial.begin(115200);

  // Create the BLE Device
  BLEDevice::init("ESP32");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
      CHARACTERISTIC_UUID,
      BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE);

  // Creates BLE Descriptor 0x2902: Client Characteristic Configuration Descriptor (CCCD)
  pCharacteristic->addDescriptor(new BLE2902());

  // Start the service
  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(false);
  pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
  BLEDevice::startAdvertising();
  Serial.println("Waiting a client connection to notify...");

  delay(2000);

  Serial.printf("Initial heap: %u\n", ESP.getFreeHeap());
}

int disconnectCounter = 0;
void loop()
{
  // disconnecting
  if (!deviceConnected && oldDeviceConnected)
  {
    pServer->startAdvertising(); // restart advertising
    // Serial.println("start advertising");
    oldDeviceConnected = deviceConnected;
    Serial.println("disconnected");
    delay(2000);
    disconnectCounter++;
    Serial.printf("Connections: %d heap: %u\n", disconnectCounter, ESP.getFreeHeap());
  }
  // connecting
  if (deviceConnected && !oldDeviceConnected)
  {
    // do stuff here on connecting
    oldDeviceConnected = deviceConnected;
    Serial.println("connected");
    delay(1000);
  }

  if (!deviceConnected)
  {
    Serial.printf("Connections: %d heap: %u\n", disconnectCounter, ESP.getFreeHeap());
    delay(1000);
  }
}

Debug Message

entry 0x403c9880
Waiting a client connection to notify...
Initial heap: 255796
Connections: 0 heap: 255796
Connections: 0 heap: 255796
Connections: 0 heap: 255796
connected
disconnected
Connections: 1 heap: 254660
Connections: 1 heap: 254660
Connections: 1 heap: 254660
Connections: 1 heap: 254660
connected
disconnected
Connections: 2 heap: 254248
Connections: 2 heap: 254248
Connections: 2 heap: 254248
Connections: 2 heap: 254248
connected
disconnected
Connections: 3 heap: 253836
Connections: 3 heap: 253836
Connections: 3 heap: 253836
Connections: 3 heap: 253836
Connections: 3 heap: 253836
Connections: 3 heap: 253836
Connections: 3 heap: 253836
Connections: 3 heap: 252168
connected
disconnected
Connections: 4 heap: 253424
Connections: 4 heap: 253424
Connections: 4 heap: 253424
Connections: 4 heap: 253424
connected
disconnected
Connections: 5 heap: 253012
Connections: 5 heap: 253012
Connections: 5 heap: 253012
Connections: 5 heap: 253012
Connections: 5 heap: 253012
Connections: 5 heap: 253012
connected
disconnected
Connections: 6 heap: 252600
Connections: 6 heap: 252600
connected
disconnected
Connections: 7 heap: 252184
Connections: 7 heap: 252184
Connections: 7 heap: 252184
Connections: 7 heap: 252184
Connections: 7 heap: 252184
connected
disconnected
Connections: 8 heap: 251776
Connections: 8 heap: 251776
Connections: 8 heap: 251776
Connections: 8 heap: 251776
Connections: 8 heap: 250104
connected
disconnected
Connections: 9 heap: 251364
Connections: 9 heap: 251364
Connections: 9 heap: 251364
Connections: 9 heap: 251364
connected
disconnected
Connections: 10 heap: 250952
Connections: 10 heap: 250952
Connections: 10 heap: 250952
Connections: 10 heap: 250952
connected
disconnected
Connections: 11 heap: 250532
Connections: 11 heap: 250532
Connections: 11 heap: 250532
Connections: 11 heap: 250532
Connections: 11 heap: 250532
Connections: 11 heap: 250532
Connections: 11 heap: 250532
connected
disconnected
Connections: 12 heap: 250120
Connections: 12 heap: 250120
Connections: 12 heap: 250120
Connections: 12 heap: 250120
Connections: 12 heap: 250120
Connections: 12 heap: 250120
Connections: 12 heap: 250120
connected
disconnected
Connections: 13 heap: 249708
Connections: 13 heap: 249708
Connections: 13 heap: 249708
connected

Other Steps to Reproduce

platformio.ini

[env:esp32-s3-devkitc-1]
platform = https://github.com/platformio/platform-espressif32.git
platform_packages =
	platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.4
	platformio/framework-arduinoespressif32-libs @ https://github.com/espressif/esp32-arduino-libs.git#idf-release/v5.1
framework = arduino
board = esp32-s3-devkitc-1
monitor_speed = 115200

I have tested this by connecting and disconnecting to the ESP32-S3 using the nRF Connect Android app.

The sketch provided is a modified version of the BLE Notify example.

This bug appears to be the same as the one found by @mirozmrzli in this discussion

Edit: The problem found by @mirozmrzli may be unrelated, as he was using idf toolchain v4.4 and latest arduino lib

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions