From 41df5f2e69508de5fc67891ee53583c1021423f5 Mon Sep 17 00:00:00 2001 From: Astha Verma Date: Tue, 19 Nov 2024 17:05:41 +0530 Subject: [PATCH] fix(nimble): fix memory leak issue in Blufi example --- nimble/host/include/host/ble_gatt.h | 2 +- .../services/gap/include/services/gap/ble_svc_gap.h | 1 + nimble/host/services/gap/src/ble_svc_gap.c | 6 ++++++ .../gatt/include/services/gatt/ble_svc_gatt.h | 2 +- nimble/host/services/gatt/src/ble_svc_gatt.c | 6 ++++++ nimble/host/src/ble_gatts.c | 13 +++++++++++++ 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/nimble/host/include/host/ble_gatt.h b/nimble/host/include/host/ble_gatt.h index 06d021782..fb278c43b 100644 --- a/nimble/host/include/host/ble_gatt.h +++ b/nimble/host/include/host/ble_gatt.h @@ -1250,7 +1250,7 @@ STAILQ_HEAD(ble_gatts_clt_cfg_list, ble_gatts_clt_cfg); * BLE_HS_ENOMEM on heap exhaustion. */ int ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs); - +void ble_gatts_free_svcs(void); #if MYNEWT_VAL(BLE_DYNAMIC_SERVICE) /** * Adds a set of services for registration. All services added diff --git a/nimble/host/services/gap/include/services/gap/ble_svc_gap.h b/nimble/host/services/gap/include/services/gap/ble_svc_gap.h index a6986ca14..88a04b2c1 100644 --- a/nimble/host/services/gap/include/services/gap/ble_svc_gap.h +++ b/nimble/host/services/gap/include/services/gap/ble_svc_gap.h @@ -58,6 +58,7 @@ int ble_svc_gap_device_key_material_set(uint8_t *session_key, uint8_t *iv); #endif void ble_svc_gap_init(void); +void ble_svc_gap_deinit(void); #ifdef __cplusplus } diff --git a/nimble/host/services/gap/src/ble_svc_gap.c b/nimble/host/services/gap/src/ble_svc_gap.c index 5f5679145..a684f1e6f 100644 --- a/nimble/host/services/gap/src/ble_svc_gap.c +++ b/nimble/host/services/gap/src/ble_svc_gap.c @@ -370,3 +370,9 @@ ble_svc_gap_init(void) SYSINIT_PANIC_ASSERT(rc == 0); #endif } + +void +ble_svc_gap_deinit(void) +{ + ble_gatts_free_svcs(); +} diff --git a/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h b/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h index f6eb65920..ea78fa61d 100644 --- a/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h +++ b/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h @@ -51,7 +51,7 @@ uint8_t ble_svc_gatt_get_csfs(void); uint8_t ble_svc_gatt_get_local_cl_supported_feat(void); void ble_svc_gatt_changed(uint16_t start_handle, uint16_t end_handle); void ble_svc_gatt_init(void); - +void ble_svc_gatt_deinit(void); #ifdef __cplusplus } #endif diff --git a/nimble/host/services/gatt/src/ble_svc_gatt.c b/nimble/host/services/gatt/src/ble_svc_gatt.c index 05890dbfe..e7267faca 100644 --- a/nimble/host/services/gatt/src/ble_svc_gatt.c +++ b/nimble/host/services/gatt/src/ble_svc_gatt.c @@ -249,3 +249,9 @@ ble_svc_gatt_init(void) ble_svc_gatt_local_cl_sup_feat |= (1 << BLE_SVC_GATT_CLI_SUP_FEAT_ROBUST_CATCHING_BIT); } } + +void +ble_svc_gatt_deinit(void) +{ + ble_gatts_free_svcs(); +} diff --git a/nimble/host/src/ble_gatts.c b/nimble/host/src/ble_gatts.c index 7c7f9fa8d..0b07da6d8 100644 --- a/nimble/host/src/ble_gatts.c +++ b/nimble/host/src/ble_gatts.c @@ -3032,6 +3032,19 @@ done: return rc; } +void ble_gatts_free_svcs(void) +{ + /* Ensure the memory is freed only if it was previously allocated */ + if (ble_gatts_svc_defs != NULL) { + /* Free the memory for the service definitions */ + free(ble_gatts_svc_defs); + /* Set the pointer to NULL to avoid dangling pointer */ + ble_gatts_svc_defs = NULL; + /* Reset the number of service definitions to 0 */ + ble_gatts_num_svc_defs = 0; + } +} + int ble_gatts_svc_set_visibility(uint16_t handle, int visible) {