Skip to content

Memory leak in Blue tooth serial with bluedroid #1798

Closed
@usrdes

Description

@usrdes

Hardware:

Board: ESP32-PICO-KIT Dev. Board
Core Installation/update date: 1 Aug 2018
IDE name: Arduino 1.8.5
Flash Frequency: 80MHz
Upload Speed: 921600

Description:

Blue tooth serial (Bluedroid) has memory leak. The sketch below is modified from serial to serial BT example.
The modification is as follows:

  1. set up timer for 1 sec timeout
  2. every sec turn on or off the bloe tooth serial.
  3. every time BTSerial is turned on or off print heap size
  4. observe that the heap size keeps diminishing, and eventually the core crashes.

Sketch: Modified serial to serial blue tooth example as follows:

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

volatile int switch_off_Bluetooth = 0;

// Timer set up
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
#define PRESCALER 50000
#define NUM_TO_COUNT_UP_TO_1_SEC  1600
#define COUNT_OF_SECS 1  
#define TIME_OUT COUNT_OF_SECS*NUM_TO_COUNT_UP_TO_1_SEC
volatile int timeout_occured=0;

void IRAM_ATTR onTimer() {
  portENTER_CRITICAL_ISR(&timerMux);
  timeout_occured = 1;
  portEXIT_CRITICAL_ISR(&timerMux);
}

void setup_timer() {
  timer = timerBegin(0, PRESCALER, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, TIME_OUT, true);
  timerAlarmEnable(timer);
}

void reset_timeout() {
    portENTER_CRITICAL(&timerMux);
    timeout_occured = 0;
    portEXIT_CRITICAL(&timerMux);
}

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  setup_timer();
  Serial.printf("Timer for a count of %d seconds of BTserial ON/OFF started",COUNT_OF_SECS);
  Serial.println(" --> ");
}

void loop() {

    if(timeout_occured) { // Turn on or off blue tooth based on timer time-out of COUNT_OF_SECS
      reset_timeout(); // first reset time out flag
      if (switch_off_Bluetooth) { // check if the BT serial needs to be turned off
        switch_off_Bluetooth = 0;
        SerialBT.flush();
        SerialBT.end(); 
        Serial.print("@ ");
        Serial.print(millis()/1000);
        Serial.printf(" secs > BTserial OFF Heap Size: %d ",ESP.getFreeHeap());
        Serial.println(" "); 
      } else { // else switch on Bluetooth serial
        switch_off_Bluetooth = 1;
        SerialBT.begin("ESP32test"); //Bluetooth device name
        Serial.print("@ ");
        Serial.print(millis()/1000);
        Serial.printf(" secs > BTserial ON  Heap Size: %d ",ESP.getFreeHeap());
        Serial.println(" "); 
      }
      
    }
    
}

Debug Messages:

The device started, now you can pair it with bluetooth!
Timer for a count of 1 seconds of BTserial ON/OFF started -->
@ 1 secs > BTserial ON Heap Size: 100308
@ 2 secs > BTserial OFF Heap Size: 169984
@ 3 secs > BTserial ON Heap Size: 95212
@ 4 secs > BTserial OFF Heap Size: 165340
@ 5 secs > BTserial ON Heap Size: 90616
@ 6 secs > BTserial OFF Heap Size: 160696
@ 7 secs > BTserial ON Heap Size: 86028
@ 8 secs > BTserial OFF Heap Size: 156060
@ 9 secs > BTserial ON Heap Size: 81340
@ 10 secs > BTserial OFF Heap Size: 152332
@ 11 secs > BTserial ON Heap Size: 77612
@ 12 secs > BTserial OFF Heap Size: 148608
@ 13 secs > BTserial ON Heap Size: 73912
@ 14 secs > BTserial OFF Heap Size: 144884
@ 15 secs > BTserial ON Heap Size: 70180
@ 16 secs > BTserial OFF Heap Size: 141160
@ 17 secs > BTserial ON Heap Size: 66508
@ 18 secs > BTserial OFF Heap Size: 137436
@ 19 secs > BTserial ON Heap Size: 62740
@ 20 secs > BTserial OFF Heap Size: 133708
@ 21 secs > BTserial ON Heap Size: 59036
@ 22 secs > BTserial OFF Heap Size: 129984
@ 23 secs > BTserial ON Heap Size: 55296
@ 24 secs > BTserial OFF Heap Size: 126264
@ 25 secs > BTserial ON Heap Size: 51552
@ 26 secs > BTserial OFF Heap Size: 122536
@ 27 secs > BTserial ON Heap Size: 47884
@ 28 secs > BTserial OFF Heap Size: 118816
@ 29 secs > BTserial ON Heap Size: 44172
@ 30 secs > BTserial OFF Heap Size: 115092
@ 31 secs > BTserial ON Heap Size: 40404
@ 32 secs > BTserial OFF Heap Size: 111368
@ 33 secs > BTserial ON Heap Size: 36664
@ 34 secs > BTserial OFF Heap Size: 107644
@ 35 secs > BTserial ON Heap Size: 32948
@ 36 secs > BTserial OFF Heap Size: 103920
@ 37 secs > BTserial ON Heap Size: 29224
@ 38 secs > BTserial OFF Heap Size: 100196
@ 39 secs > BTserial ON Heap Size: 25504
@ 40 secs > BTserial OFF Heap Size: 96472
@ 41 secs > BTserial ON Heap Size: 21804
@ 42 secs > BTserial OFF Heap Size: 92748
@ 43 secs > BTserial ON Heap Size: 18076
@ 44 secs > BTserial OFF Heap Size: 89024
@ 45 secs > BTserial ON Heap Size: 14384
@ 46 secs > BTserial OFF Heap Size: 85300
@ 47 secs > BTserial ON Heap Size: 10696
@ 48 secs > BTserial OFF Heap Size: 81576
@ 49 secs > BTserial ON Heap Size: 6928
@ 50 secs > BTserial OFF Heap Size: 77852
@ 53 secs > BTserial ON Heap Size: 3112
@ 53 secs > BTserial OFF Heap Size: 74132
Guru Meditation Error: Core 0 panic'ed (StoreProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x4000c46c PS : 0x00060e30 A0 : 0x801139de A1 : 0x3ffdaf90
A2 : 0x00000000 A3 : 0x00000000 A4 : 0x0000001c A5 : 0x00000000
A6 : 0x40179f3c A7 : 0x00000001 A8 : 0x800d70bd A9 : 0x3ffdaf50
A10 : 0x3ffe8ca8 A11 : 0x00000000 A12 : 0x00000001 A13 : 0x3ffe7370
A14 : 0x3ffe15f4 A15 : 0x00000084 SAR : 0x00000008 EXCCAUSE: 0x0000001d
EXCVADDR: 0x00000000 LBEG : 0x4000c46c LEND : 0x4000c477 LCOUNT : 0x00000000
Backtrace: 0x4000c46c:0x3ffdaf90 0x401139db:0x3ffdafa0 0x401142b1:0x3ffdafc0
0x400ef8f5:0x3ffdafe0 0x400ef92f:0x3ffdb000
Rebooting...
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 188777542, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:808
load:0x40078000,len:6084
load:0x40080000,len:6696
entry 0x400802e4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions