Skip to content

Commit 7e268ad

Browse files
committed
Merge branch 'bugfix/btdm_mem_leak_of_bt_dynamic_memory' into 'master'
component/bt: Fix bug of bt dynamic memory See merge request idf/esp-idf!2053
2 parents dc66940 + 3ee5b49 commit 7e268ad

File tree

9 files changed

+146
-2
lines changed

9 files changed

+146
-2
lines changed

components/bt/bluedroid/main/bte_init.c

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ void BTE_InitStack(void)
173173
#endif // PAN
174174
#endif // BNEP Included
175175

176-
177176
//AVDT and its profiles
178177
#if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
179178
A2D_Init();
@@ -229,6 +228,12 @@ void BTE_InitStack(void)
229228
memset((void *)bta_dm_di_cb_ptr, 0, sizeof(tBTA_DM_DI_CB));
230229
//memset((void *)bta_prm_cb_ptr, 0, sizeof(tBTA_PRM_CB));
231230
//memset((void *)bta_ag_cb_ptr, 0, sizeof(tBTA_AG_CB));
231+
#if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
232+
if ((bta_jv_cb_ptr = (tBTA_JV_CB *)osi_malloc(sizeof(tBTA_JV_CB))) == NULL) {
233+
return;
234+
}
235+
memset((void *)bta_jv_cb_ptr, 0, sizeof(tBTA_JV_CB));
236+
#endif //JV
232237
#if BTA_HS_INCLUDED == TRUE
233238
memset((void *)bta_hs_cb_ptr, 0, sizeof(tBTA_HS_CB));
234239
#endif
@@ -253,7 +258,7 @@ void BTE_InitStack(void)
253258
#if BTA_HH_INCLUDED==TRUE
254259
if ((bta_hh_cb_ptr = (tBTA_HH_CB *)osi_malloc(sizeof(tBTA_HH_CB))) == NULL) {
255260
return;
256-
}
261+
}
257262
memset((void *)bta_hh_cb_ptr, 0, sizeof(tBTA_HH_CB));
258263
#endif
259264
#if BTA_HL_INCLUDED==TRUE
@@ -277,3 +282,76 @@ void BTE_InitStack(void)
277282

278283
#endif // BTA_INCLUDED == TRUE
279284
}
285+
286+
/*****************************************************************************
287+
**
288+
** Function BTE_DeinitStack
289+
**
290+
** Description Deinitialize control block memory for each component.
291+
**
292+
** Note: This API must be called
293+
** after freeing the BTU Task.
294+
**
295+
** Returns void
296+
**
297+
******************************************************************************/
298+
void BTE_DeinitStack(void)
299+
{
300+
//BTA Modules
301+
#if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
302+
#if GATTS_INCLUDED == TRUE
303+
osi_free(bta_gatts_cb_ptr);
304+
bta_gatts_cb_ptr = NULL;
305+
#endif
306+
#if GATTC_INCLUDED==TRUE
307+
osi_free(bta_gattc_cb_ptr);
308+
bta_gattc_cb_ptr = NULL;
309+
#endif
310+
#if BTA_HH_INCLUDED==TRUE
311+
osi_free(bta_hh_cb_ptr);
312+
bta_hh_cb_ptr = NULL;
313+
#endif
314+
#if BTA_AV_INCLUDED==TRUE
315+
osi_free(bta_av_cb_ptr);
316+
bta_av_cb_ptr = NULL;
317+
#endif
318+
#if BTA_AR_INCLUDED==TRUE
319+
osi_free(bta_ar_cb_ptr);
320+
bta_ar_cb_ptr = NULL;
321+
#endif
322+
#if BTA_SDP_INCLUDED == TRUE
323+
osi_free(bta_sdp_cb_ptr);
324+
bta_sdp_cb_ptr = NULL;
325+
#endif
326+
#if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
327+
osi_free(bta_jv_cb_ptr);
328+
bta_jv_cb_ptr = NULL;
329+
#endif //JV
330+
osi_free(bta_dm_di_cb_ptr);
331+
bta_dm_di_cb_ptr = NULL;
332+
osi_free(bta_dm_search_cb_ptr);
333+
bta_dm_search_cb_ptr = NULL;
334+
osi_free(bta_dm_cb_ptr);
335+
bta_dm_cb_ptr = NULL;
336+
osi_free(bta_sys_cb_ptr);
337+
bta_sys_cb_ptr = NULL;
338+
#endif // BTA_INCLUDED == TRUE
339+
340+
#if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE && AVCT_DYNAMIC_MEMORY == TRUE)
341+
osi_free(avct_cb_ptr);
342+
avct_cb_ptr = NULL;
343+
#endif
344+
345+
#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE && AVDT_DYNAMIC_MEMORY == TRUE)
346+
osi_free(avdt_cb_ptr);
347+
avdt_cb_ptr = NULL;
348+
#endif
349+
350+
#if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
351+
AVRC_Deinit();
352+
#endif
353+
354+
#if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
355+
A2D_Deinit();
356+
#endif
357+
}

components/bt/bluedroid/stack/a2dp/a2d_api.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,22 @@ void A2D_Init(void)
391391
#endif
392392
}
393393

394+
/*******************************************************************************
395+
**
396+
** Function A2D_Deinit
397+
**
398+
** Description This function is called to deinitialize the control block
399+
** for this layer.
400+
**
401+
** Returns void
402+
**
403+
*******************************************************************************/
404+
void A2D_Deinit(void)
405+
{
406+
#if (A2D_DYNAMIC_MEMORY)
407+
osi_free(a2d_cb_ptr);
408+
a2d_cb_ptr = NULL;
409+
#endif /* #if (A2D_DYNAMIC_MEMORY) */
410+
}
411+
394412
#endif /* #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) */

components/bt/bluedroid/stack/avrc/avrc_sdp.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,23 @@ void AVRC_Init(void)
357357
#endif
358358
}
359359

360+
/*******************************************************************************
361+
**
362+
** Function AVRC_Deinit
363+
**
364+
** Description This function is called at stack shotdown to free the
365+
** control block (if using dynamic memory), and deinitializes the
366+
** control block and tracing level.
367+
**
368+
** Returns void
369+
**
370+
*******************************************************************************/
371+
void AVRC_Deinit(void)
372+
{
373+
#if AVRC_DYNAMIC_MEMORY
374+
osi_free(avrc_cb_ptr);
375+
avrc_cb_ptr = NULL;
376+
#endif /* #if AVRC_DYNAMIC_MEMORY */
377+
}
378+
360379
#endif /* #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE) */

components/bt/bluedroid/stack/btu/btu_init.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ void btu_free_core(void)
118118
// Free the mandatory core stack components
119119
l2c_free();
120120

121+
#if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
122+
sdp_deinit();
123+
#endif
124+
121125
#if BLE_INCLUDED == TRUE
122126
#if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
123127
gatt_free();

components/bt/bluedroid/stack/btu/btu_task.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ extern void avdt_rcv_sync_info (BT_HDR *p_buf);
8686
//#endif
8787

8888
extern void BTE_InitStack(void);
89+
extern void BTE_DeinitStack(void);
8990

9091
/* Define BTU storage area
9192
*/
@@ -314,6 +315,7 @@ void btu_task_shut_down(void)
314315
#if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
315316
bta_sys_free();
316317
#endif
318+
BTE_DeinitStack();
317319

318320
btu_free_core();
319321
}

components/bt/bluedroid/stack/include/a2d_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,6 @@ extern UINT8 A2D_BitsSet(UINT8 num);
251251
**
252252
*******************************************************************************/
253253
extern void A2D_Init(void);
254+
extern void A2D_Deinit(void);
254255
#endif ///A2D_INCLUDED
255256
#endif /* A2D_API_H */

components/bt/bluedroid/stack/include/avrc_api.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,19 @@ extern UINT8 AVRC_SetTraceLevel (UINT8 new_level);
554554
*******************************************************************************/
555555
extern void AVRC_Init(void);
556556

557+
/*******************************************************************************
558+
**
559+
** Function AVRC_Deinit
560+
**
561+
** Description This function is called at stack shotdown to free the
562+
** control block (if using dynamic memory), and deinitializes the
563+
** control block and tracing level.
564+
**
565+
** Returns void
566+
**
567+
*******************************************************************************/
568+
extern void AVRC_Deinit(void);
569+
557570
/*******************************************************************************
558571
**
559572
** Function AVRC_ParsCommand

components/bt/bluedroid/stack/sdp/include/sdpint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ extern tSDP_CB *sdp_cb_ptr;
237237

238238
/* Functions provided by sdp_main.c */
239239
extern void sdp_init (void);
240+
extern void sdp_deinit (void);
240241
extern void sdp_disconnect (tCONN_CB *p_ccb, UINT16 reason);
241242

242243
#if (defined(SDP_DEBUG) && SDP_DEBUG == TRUE)

components/bt/bluedroid/stack/sdp/sdp_main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ void sdp_init (void)
138138
}
139139
}
140140

141+
void sdp_deinit (void)
142+
{
143+
#if SDP_DYNAMIC_MEMORY
144+
osi_free(sdp_cb_ptr);
145+
sdp_cb_ptr = NULL;
146+
#endif /* #if SDP_DYNAMIC_MEMORY */
147+
}
148+
141149
#if (defined(SDP_DEBUG) && SDP_DEBUG == TRUE)
142150
/*******************************************************************************
143151
**

0 commit comments

Comments
 (0)