This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
工具/软件:Code Composer Studio
问题与释放 iCall_malloc 分配的内存有关
在 CC2652RB_LaunchXL 的 SimplePeripheral 示例中,广告回调会向中的应用程序发送事件:
静态空 SimplePeripheral_advCallback (uint32_t 事件、void * pBuf、uintptr_t arg) { spGapAdvEventData_t * pData = iCall_malloc (sizeof (spGapAdvEventData_t))); if (pData) { pData->event = event; pData->pBuf = pBuf; if (SimplePeripheral_enqueueMsg (SP_ADV_EVT、pData)!=成功) { iCall_free (pData); } }
因此、由 iCall_malloc 为 pData 变量分配内存
在 SimplePeripheral_enqueueMsg 中、使用 iCall_malloc 创建和分配新变量 pMsg
static status_t SimplePeripheral_enqueueMsg (uint8_t 事件、void *pData) { uint8_t 成功; spEvt_t * pMsg = iCall_malloc (sizeof (spEvt_t))); //创建消息的动态指针。 if (pMsg) { pMsg->EVENT = EVENT; pMsg->pData = pData; //将消息排队。 Success = Util_enqueueMsg (appMsgQueueHandle、syncEvent、(uint8_t *) pMsg); 返回(成功)? 成功:失败 ;} 返回(bleMemAllocError); }
然后 、当应用程序在 SimplePeripheral_taskFxn 中接收到消息时、变量 pMsg 被释放:
//如果 RTOS 队列不为空,则处理应用程序消息。 IF (事件和 SP_queue_EVT) { while (!Queue_empty (appMsgQueueHandle)) { spEvt_t *pMsg =(spEvt_t *) Util_dequeueMsg (appMsgQueueHandle); if (pMsg) { //处理消息。 SimplePeripheral_processAppMsg (pMsg); //从消息中释放空间。 iCall_free (pMsg); } }
那么问题是:何时 以及如何释放分配给进程开始时创建的 pData 变量的内存?
感谢你的帮助。
Frederic
您好 Frederic、
pData 在 SimplePeripheral_processAppMsg ()的末尾被释放:
静态空 SimplePeripheral_processAppMsg (spEvt_t *pMsg) { bool dealloc = true; //…… switch (pMsg->EVENT) { //... 案例 SP_ADV_EVT: SimplePeripheral_processAdvEvent((spGapAdvEventData_t*)(pMsg->pData)); 中断; //... //如果消息数据存在,则释放该数据,如果 ((dalloc == true)&&(pMsg->pData!= NULL) { iCall_free (pMsg->pData); }
非常感谢 Marie。
我确实错过了这条线路。
此致。
Frederic