embedded
runtime_error
ai_generated
true
FreeRTOS:在prvCheckHeap()第234行检测到堆损坏
FreeRTOS: heap corruption detected in prvCheckHeap() at line 234
ID: embedded/rtos-heap-corruption
72%修复率
88%置信度
1证据数
2024-01-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| FreeRTOSv202212.01 | active | — | — | — |
| STM32Cube_FW_F4_V1.27.0 | active | — | — | — |
| ARM GCC 12.2.1 | active | — | — | — |
根因分析
内存损坏由缓冲区溢出、重复释放或释放后使用堆分配对象引起。
English
Memory corruption caused by buffer overflow, double free, or use-after-free in heap-allocated objects.
官方文档
https://www.freertos.org/Documentation/02-Kernel/02-Kernel-features/03-Memory-management/01-Memory-management解决方案
-
Enable heap monitoring by setting configUSE_MALLOC_FAILED_HOOK to 1 and implementing vApplicationMallocFailedHook() to log the task name and stack trace.
-
Use static allocation for all tasks and queues: replace pvPortMalloc() with static buffers defined at compile time, e.g., StackType_t taskStack[256]; StaticTask_t taskBuffer;
-
Add memory guard words before and each allocated block: #define GUARD 0xDEADBEEF; and check them after free to detect buffer overflow.
无效尝试
常见但无效的做法:
-
Increasing total heap size in FreeRTOSConfig.h (configTOTAL_HEAP_SIZE)
90% 失败
Larger heap only delays corruption detection; does not fix the root cause of memory mismanagement.
-
Adding assert() statements around every malloc/free call
80% 失败
Asserts only halt execution; they do not prevent corruption and may mask intermittent issues.
-
Switching to heap_4.c from heap_2.c
85% 失败
Different heap implementation may change behavior but cannot fix logical errors like double free.