Skip to content

BLE library has a memory leak. #4753

Closed
Closed
@ushiboy

Description

@ushiboy

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:

#include "src/BLEDevice.h"

static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");

uint32_t count = 0;
static boolean doConnect = false;
static boolean doScan = false;
static BLEAdvertisedDevice *myDevice;

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();
  return true;
}

class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
  void onResult(BLEAdvertisedDevice advertisedDevice)
  {
    doScan = true;
    if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID))
    {
      BLEDevice::getScan()->stop();
      if (myDevice != nullptr)
      {
        delete myDevice;
      }
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = false;
    }
  }
};

void setup()
{
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");
  BLEScan *pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
}

void loop()
{
  if (doConnect)
  {
    count++;
    Serial.printf("Count: %d\n", count);
    Serial.printf("Free Heap Size: %d\n", esp_get_free_heap_size());
    Serial.printf("System Free Heap Size: %d\n", system_get_free_heap_size());
    Serial.printf("Minimum Free Heap Size: %d\n", esp_get_minimum_free_heap_size());

    connectToServer();
    doConnect = false;
    doScan = true;
  }
  else if (doScan)
  {
    doScan = false;
    BLEDevice::getScan()->start(0);
  }

  delay(1000);
}

Debug Messages:

Count: 47
Free Heap Size: 6896
System Free Heap Size: 6896
Minimum Free Heap Size: 1812
[V][BLEDevice.cpp:60] createClient(): >> createClient
[V][BLEDevice.cpp:66] createClient(): << createClient
[V][BLEClient.cpp:96] connect(): >> connect(80:7d:3a:dc:d5:4a)
[I][BLEDevice.cpp:614] addPeerDevice(): add conn_id: 46, GATT role: client
[V][FreeRTOS.cpp:189] take(): Semaphore taking: name: RegEvt (0x3fffee78), owner: <N/A> for connect
[V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: RegEvt (0x3fffee78), owner: connect
[V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: RegEvt (0x3fffee78), owner: connect for connect
[V][BLEUtils.cpp:951] gattClientEventTypeToString(): Unknown GATT Client event type: 0
[D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:951] gattClientEventTypeToString(): Unknown GATT Client event type: 0
[V][BLEUtils.cpp:1283] dumpGattClientEvent(): GATT Event: Unknown
[V][BLEUtils.cpp:951] gattClientEventTypeToString(): Unknown GATT Client event type: 0
[D][BLEClient.cpp:160] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[V][FreeRTOS.cpp:143] give(): Semaphore giving: name: RegEvt (0x3fffee78), owner: connect
[V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: RegEvt (0x3fffee78), owner: <N/A>
[V][FreeRTOS.cpp:189] take(): Semaphore taking: name: OpenEvt (0x3fffff4c), owner: <N/A> for connect
[V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: OpenEvt (0x3fffff4c), owner: connect
[V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: OpenEvt (0x3fffff4c), owner: connect for connect

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions