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.

[参考译文] CC1352P7:有关 malloc 失败的问题。

Guru**** 2392905 points
Other Parts Discussed in Thread: CC1352P7, SYSCONFIG

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

https://e2e.ti.com/support/wireless-connectivity/sub-1-ghz-group/sub-1-ghz/f/sub-1-ghz-forum/1402959/cc1352p7-problem-about-malloc-fails

器件型号:CC1352P7
主题中讨论的其他器件: SysConfig

工具与软件:

尊敬的 TI:

我使用 simplelink_cc13x2_26x2_SDK_5_20_00_52 和 CC1352P7。

现在我有关于 malloc 函数的问题。

在我的应用中、我需要使用 malloc 4~6次。

伪代码如下所示:

void foo(void)
{
    uint8_t *p1 = malloc(hundred bytes);
    uint8_t *p2 = malloc(hundred bytes);
    uint8_t *p3 = malloc(hundred bytes);
    uint8_t *p4 = malloc(hundred bytes);
    uint8_t *p5 = malloc(hundred bytes);
    uint8_t *p6 = malloc(hundred bytes);
    
    /*Some code using the above 6 pointers*/
    
    if (p1 != NULL) {free(p1);}
    if (p2 != NULL) {free(p2);}
    if (p3 != NULL) {free(p3);}
    if (p4 != NULL) {free(p4);}
    if (p5 != NULL) {free(p5);}
    if (p6 != NULL) {free(p6);}
}

以上代码每秒执行一次。

出现这种现象的时候、程序可能会在 malloc 内暂停、然后在几秒钟内重新启动。

有时、程序可能运行几个小时然后停止、而有时、程序可能只运行几秒钟然后停止。

下面是程序运行几秒钟的打印日志。

日志由3次分配组成。

这是前4个分配表:

一秒钟之后、第二个6个分配表:

一秒钟之后、第三个6个分配表:

您可以看到、在4个 mallocs 后、程序发生异常、它将暂停、然后重新启动。

我已经测试了几天这个问题。 每次程序暂停时、它都会在 malloc 函数中停止。

在打印日志中、我们可以从 malloc 看到 RAM 中每个指针的地址、并且地址指示应该不会出现内存泄漏。

程序有大约40KB 的堆大小、打印日志中的 malloc 总大小约为11kB、因此有足够的可用堆大小。

那么、如何进一步调查这个问题呢、例如、我可以实时知道堆的剩余大小、或者使用任何其他方法吗?

谢谢你。