From e2c02966d23bdec8bf69d58af98bcc118b0cec58 Mon Sep 17 00:00:00 2001 From: Zhang Hai Peng Date: Mon, 7 Apr 2025 21:14:19 +0800 Subject: [PATCH] fix(ble/bluedrooid): Fixed memory leak issue when deinit the host (cherry picked from commit 6becf74cbb86df03c202f0df66b9e418464a76a8) Co-authored-by: zhanghaipeng --- components/bt/host/bluedroid/device/controller.c | 13 ++++++++++++- components/bt/host/bluedroid/main/bte_main.c | 10 +++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/components/bt/host/bluedroid/device/controller.c b/components/bt/host/bluedroid/device/controller.c index 817838fe61..537e0e5030 100644 --- a/components/bt/host/bluedroid/device/controller.c +++ b/components/bt/host/bluedroid/device/controller.c @@ -101,6 +101,7 @@ static controller_local_param_t *controller_param_ptr; #define AWAIT_COMMAND(command) future_await(controller_param.hci->transmit_command_futured(command)) +static bool loaded = false; // Module lifecycle functions static void start_up(void) @@ -348,6 +349,17 @@ static void shut_down(void) controller_param.readable = false; } +#if (BT_BLE_DYNAMIC_ENV_MEMORY == TRUE) +void free_controller_param(void) +{ + if (controller_param_ptr) { + osi_free(controller_param_ptr); + controller_param_ptr = NULL; + loaded = false; + } +} +#endif + static bool get_is_ready(void) { return controller_param.readable; @@ -632,7 +644,6 @@ static const controller_t interface = { const controller_t *controller_get_interface(void) { - static bool loaded = false; if (!loaded) { loaded = true; #if (BT_BLE_DYNAMIC_ENV_MEMORY == TRUE) diff --git a/components/bt/host/bluedroid/main/bte_main.c b/components/bt/host/bluedroid/main/bte_main.c index c47bdc15f9..c974037ef9 100644 --- a/components/bt/host/bluedroid/main/bte_main.c +++ b/components/bt/host/bluedroid/main/bte_main.c @@ -64,6 +64,9 @@ static void bte_main_enable(void); bluedroid_init_done_cb_t bluedroid_init_done_cb; extern void osi_mem_dbg_init(void); +#if (BT_BLE_DYNAMIC_ENV_MEMORY == TRUE) +extern void free_controller_param(void); +#endif /****************************************************************************** ** ** Function bte_main_boot_entry @@ -85,7 +88,7 @@ int bte_main_boot_entry(bluedroid_init_done_cb_t cb) osi_init(); - //Enbale HCI + //Enable HCI bte_main_enable(); return 0; @@ -105,6 +108,11 @@ void bte_main_shutdown(void) #if (BLE_INCLUDED == TRUE) BTA_VendorCleanup(); #endif + +#if (BT_BLE_DYNAMIC_ENV_MEMORY == TRUE) + free_controller_param(); +#endif + bte_main_disable(); osi_deinit();