ble gatt server memory leak when connect/disconnect

YektaD
Posts: 1
Joined: Tue Aug 22, 2023 1:21 pm

ble gatt server memory leak when connect/disconnect

Postby YektaD » Tue Aug 22, 2023 1:40 pm

Hello everyone,
I was working on a project and i realised memory leak happens when i connect and disconnect with my phone.
To generate this problem, i created a ble gatt server example. The only change i made is printing free internal ram space.

This is at the end of the main function:
  1. while(1){
  2.  
  3.         ESP_LOGI("app_main", "Free Internal Ram Size: %d\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  4.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  5. }
And these are inside "gatts_profile_a_event_handler":
  1. case ESP_GATTS_CONNECT_EVT: {
  2.         ESP_LOGI("ESP_GATTS_CONNECT_EVT", "Free heap1: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  3.         esp_ble_conn_update_params_t conn_params = {0};
  4.         ESP_LOGI("ESP_GATTS_CONNECT_EVT", "Free heap2: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  5.         memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
  6.         ESP_LOGI("ESP_GATTS_CONNECT_EVT", "Free heap3: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  7.         /* For the IOS system, please reference the apple official documents about the ble connection parameters restrictions. */
  8.         conn_params.latency = 0;
  9.         conn_params.max_int = 0x20;    // max_int = 0x20*1.25ms = 40ms
  10.         conn_params.min_int = 0x10;    // min_int = 0x10*1.25ms = 20ms
  11.         conn_params.timeout = 400;    // timeout = 400*10ms = 4000ms
  12.         ESP_LOGI("ESP_GATTS_CONNECT_EVT", "Free heap4: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  13.         ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:",
  14.                  param->connect.conn_id,
  15.                  param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
  16.                  param->connect.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5]);
  17.         gl_profile_tab[PROFILE_A_APP_ID].conn_id = param->connect.conn_id;
  18.         ESP_LOGI("ESP_GATTS_CONNECT_EVT", "Free heap5: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  19.         //start sent the update connection parameters to the peer device.
  20.         esp_ble_gap_update_conn_params(&conn_params);
  21.         ESP_LOGI("ESP_GATTS_CONNECT_EVT", "Free heap6: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  22.         break;
  23.     }
  24.     case ESP_GATTS_DISCONNECT_EVT:
  25.         ESP_LOGI("ESP_GATTS_DISCONNECT_EVT", "Free heap1: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  26.         ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason);
  27.        ESP_LOGI("ESP_GATTS_DISCONNECT_EVT", "Free heap2: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  28.         esp_ble_gap_start_advertising(&adv_params);
  29.         ESP_LOGI("ESP_GATTS_DISCONNECT_EVT", "Free heap3: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  30.         break;
And these are the results:

Code: Select all

I (6052) app_main: Free Internal Ram Size: 241840

I (7052) app_main: Free Internal Ram Size: 241840

I (8052) app_main: Free Internal Ram Size: 241840

I (9052) app_main: Free Internal Ram Size: 241840

I (10052) app_main: Free Internal Ram Size: 241840

I (10822) ESP_GATTS_CONNECT_EVT: Free heap1: 238148
I (10822) ESP_GATTS_CONNECT_EVT: Free heap2: 238148
I (10822) ESP_GATTS_CONNECT_EVT: Free heap3: 238148
I (10822) ESP_GATTS_CONNECT_EVT: Free heap4: 238148
I (10832) GATTS_DEMO: ESP_GATTS_CONNECT_EVT, conn_id 0, remote 59:ba:07:d0:89:8d:
I (10842) ESP_GATTS_CONNECT_EVT: Free heap5: 238148
I (10842) ESP_GATTS_CONNECT_EVT: Free heap6: 238096
I (10852) GATTS_DEMO: CONNECT_EVT, conn_id 0, remote 59:ba:07:d0:89:8d:
I (11052) app_main: Free Internal Ram Size: 238696

I (11262) GATTS_DEMO: update connection params status = 0, min_int = 16, max_int = 32,conn_int = 24,latency = 0, timeout = 400
I (11622) GATTS_DEMO: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 6,latency = 0, timeout = 500
I (11792) GATTS_DEMO: ESP_GATTS_MTU_EVT, MTU 500
I (11792) GATTS_DEMO: ESP_GATTS_MTU_EVT, MTU 500
I (11862) GATTS_DEMO: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 24,latency = 0, timeout = 400
I (12052) app_main: Free Internal Ram Size: 238696

I (13052) app_main: Free Internal Ram Size: 238696

I (14052) app_main: Free Internal Ram Size: 238696

I (15052) app_main: Free Internal Ram Size: 238696

I (16052) app_main: Free Internal Ram Size: 238696

W (16662) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x13
I (16662) ESP_GATTS_DISCONNECT_EVT: Free heap1: 240432
I (16662) GATTS_DEMO: ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x13
I (16672) ESP_GATTS_DISCONNECT_EVT: Free heap2: 240432
I (16672) ESP_GATTS_DISCONNECT_EVT: Free heap3: 240380
I (17052) app_main: Free Internal Ram Size: 240804

I (18052) app_main: Free Internal Ram Size: 240804

I (19052) app_main: Free Internal Ram Size: 240804

I (20052) app_main: Free Internal Ram Size: 240804

I (20722) ESP_GATTS_CONNECT_EVT: Free heap1: 238072
I (20722) ESP_GATTS_CONNECT_EVT: Free heap2: 238072
I (20722) ESP_GATTS_CONNECT_EVT: Free heap3: 238072
I (20722) ESP_GATTS_CONNECT_EVT: Free heap4: 238072
I (20732) GATTS_DEMO: ESP_GATTS_CONNECT_EVT, conn_id 0, remote 6a:f4:b0:97:7d:f3:
I (20742) ESP_GATTS_CONNECT_EVT: Free heap5: 238072
I (20742) ESP_GATTS_CONNECT_EVT: Free heap6: 238016
I (20752) GATTS_DEMO: CONNECT_EVT, conn_id 0, remote 6a:f4:b0:97:7d:f3:
I (21052) app_main: Free Internal Ram Size: 238332

I (21192) GATTS_DEMO: update connection params status = 0, min_int = 16, max_int = 32,conn_int = 24,latency = 0, timeout = 400
I (21552) GATTS_DEMO: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 6,latency = 0, timeout = 500
I (21722) GATTS_DEMO: ESP_GATTS_MTU_EVT, MTU 500
I (21722) GATTS_DEMO: ESP_GATTS_MTU_EVT, MTU 500
I (21792) GATTS_DEMO: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 24,latency = 0, timeout = 400
I (22052) app_main: Free Internal Ram Size: 238332

I (23052) app_main: Free Internal Ram Size: 238332

I (24052) app_main: Free Internal Ram Size: 238332

I (25052) app_main: Free Internal Ram Size: 238332

I (26052) app_main: Free Internal Ram Size: 238332

W (26082) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x13
I (26082) ESP_GATTS_DISCONNECT_EVT: Free heap1: 240084
I (26082) GATTS_DEMO: ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x13
I (26092) ESP_GATTS_DISCONNECT_EVT: Free heap2: 240084
I (26092) ESP_GATTS_DISCONNECT_EVT: Free heap3: 240032
I (27052) app_main: Free Internal Ram Size: 240456

I (28052) app_main: Free Internal Ram Size: 240456

I (29052) app_main: Free Internal Ram Size: 240456

I (29692) ESP_GATTS_CONNECT_EVT: Free heap1: 237740
I (29692) ESP_GATTS_CONNECT_EVT: Free heap2: 237740
I (29692) ESP_GATTS_CONNECT_EVT: Free heap3: 237740
I (29692) ESP_GATTS_CONNECT_EVT: Free heap4: 237740
I (29702) GATTS_DEMO: ESP_GATTS_CONNECT_EVT, conn_id 0, remote 53:3e:6e:ec:75:e7:
I (29712) ESP_GATTS_CONNECT_EVT: Free heap5: 237740
I (29712) ESP_GATTS_CONNECT_EVT: Free heap6: 237684
I (29722) GATTS_DEMO: CONNECT_EVT, conn_id 0, remote 53:3e:6e:ec:75:e7:
I (30052) app_main: Free Internal Ram Size: 238000

I (30132) GATTS_DEMO: update connection params status = 0, min_int = 16, max_int = 32,conn_int = 24,latency = 0, timeout = 400
I (30492) GATTS_DEMO: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 6,latency = 0, timeout = 500
I (30662) GATTS_DEMO: ESP_GATTS_MTU_EVT, MTU 500
I (30662) GATTS_DEMO: ESP_GATTS_MTU_EVT, MTU 500
I (30732) GATTS_DEMO: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 24,latency = 0, timeout = 400
I (31052) app_main: Free Internal Ram Size: 238000

I (32052) app_main: Free Internal Ram Size: 238000

I (33052) app_main: Free Internal Ram Size: 238000

I (34052) app_main: Free Internal Ram Size: 238000

I (35052) app_main: Free Internal Ram Size: 238000

W (35112) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x13
I (35112) ESP_GATTS_DISCONNECT_EVT: Free heap1: 239736
I (35112) GATTS_DEMO: ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x13
I (35122) ESP_GATTS_DISCONNECT_EVT: Free heap2: 239736
I (35122) ESP_GATTS_DISCONNECT_EVT: Free heap3: 239684
I (36052) app_main: Free Internal Ram Size: 240108

I (37052) app_main: Free Internal Ram Size: 240108

I (38052) app_main: Free Internal Ram Size: 240108

I (39052) app_main: Free Internal Ram Size: 240108

Results are same for ESP IDF 5.0.3 and 4.3.5

Do anyone has any idea?

Who is online

Users browsing this forum: Majestic-12 [Bot], romboidale, sohaib.q and 93 guests