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.

[参考译文] CCS/CC2642R-Q1:SimplePeripheral 中的 iCall_malloc

Guru**** 1986165 points
Other Parts Discussed in Thread: CC2652RB
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/898129/ccs-cc2642r-q1-icall_malloc-in-simpleperipheral

器件型号:CC2642R-Q1
主题中讨论的其他器件:CC2652RB

工具/软件: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

x 出现错误。请重试或与管理员联系。