fix(nimble): fix memory leak issue in Blufi example

This commit is contained in:
Astha Verma
2024-11-27 15:13:49 +05:30
committed by Rahul Tank
parent dd6dfb798b
commit fadd347a41
4 changed files with 47 additions and 5 deletions

View File

@@ -44,6 +44,7 @@ void esp_blufi_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *a
/* Initialise gatt server */ /* Initialise gatt server */
int esp_blufi_gatt_svr_init(void); int esp_blufi_gatt_svr_init(void);
int esp_blufi_gatt_svr_deinit(void);
void esp_blufi_btc_init(void); void esp_blufi_btc_init(void);
void esp_blufi_btc_deinit(void); void esp_blufi_btc_deinit(void);
#endif #endif

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -240,6 +240,32 @@ static void init_gatt_values(void)
} }
static void deinit_gatt_values(void)
{
int i = 0;
const struct ble_gatt_svc_def *svc;
const struct ble_gatt_chr_def *chr;
const struct ble_gatt_dsc_def *dsc;
for (svc = gatt_svr_svcs; svc && svc->uuid; svc++) {
for (chr = svc->characteristics; chr && chr->uuid; chr++) {
if (i < SERVER_MAX_VALUES && gatt_values[i].buf != NULL) {
os_mbuf_free(gatt_values[i].buf); /* Free the buffer */
gatt_values[i].buf = NULL; /* Nullify the pointer to avoid dangling references */
}
++i;
for (dsc = chr->descriptors; dsc && dsc->uuid; dsc++) {
if (i < SERVER_MAX_VALUES && gatt_values[i].buf != NULL) {
os_mbuf_free(gatt_values[i].buf); /* Free the buffer */
gatt_values[i].buf = NULL; /* Nullify the pointer to avoid dangling references */
}
++i;
}
}
}
}
int esp_blufi_gatt_svr_init(void) int esp_blufi_gatt_svr_init(void)
{ {
int rc; int rc;
@@ -260,6 +286,18 @@ int esp_blufi_gatt_svr_init(void)
return 0; return 0;
} }
int esp_blufi_gatt_svr_deinit(void)
{
deinit_gatt_values();
ble_gatts_free_svcs();
/* Deinitialize BLE GATT and GAP services */
ble_svc_gatt_deinit();
ble_svc_gap_deinit();
return 0;
}
static int static int
esp_blufi_gap_event(struct ble_gap_event *event, void *arg) esp_blufi_gap_event(struct ble_gap_event *event, void *arg)
{ {

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@@ -227,8 +227,11 @@ esp_err_t esp_blufi_host_deinit(void)
{ {
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
esp_blufi_gatt_svr_deinit();
ret = nimble_port_stop(); ret = nimble_port_stop();
if (ret != ESP_OK) {
return ret;
}
if (ret == 0) { if (ret == 0) {
esp_nimble_deinit(); esp_nimble_deinit();
} }