我在
STM32F103中使用了RTTHREAD,同时运行多个任务, 然后在任务A中malloc一块内存用于存储字符串数据,使用完成后立刻释放,结果发现释放时有时有发生hard fault问题,定位在r_free函数处。
再通过查看发生错误时的log显示:该数据的内存地址与正在运行任务B地址重叠的,导致释放时就发生了hard fault错误。
问题在于为什么rt_malloc申请的内存地址会与正在运行的任务B重叠?
LOG如下:allocate memory at 0x20003eb0 第一次申请了512大小的内存是任务B本身的,第二次就是任务A内的函数申请的。
[02/14 17:17:55.331]allocate memory at 0x20003e2c, size: 132
[02/14 17:17:55.331]malloc size 500
[02/14 17:17:55.331]allocate memory at 0x20003eb0, size: 512
[02/14 17:17:55.331]D/app short thread run...
[02/14 17:17:55.956]malloc size 32
[02/14 17:17:55.956]allocate memory at 0x20003eb0, size: 44
[02/14 17:17:55.987]release memory 0x20003eb0, size: 44
[02/14 17:17:55.987]psr: 0x21000000
[02/14 17:17:55.987]r00: 0x20001278
[02/14 17:17:55.987]r01: 0x28029de7
[02/14 17:17:55.987]r02: 0x00002c2c
[02/14 17:17:55.987]r03: 0x000063f4
[02/14 17:17:55.987]r04: 0x20003ea4
[02/14 17:17:55.987]r05: 0x20000024
[02/14 17:17:55.987]r06: 0x20000024
[02/14 17:17:55.987]r07: 0xdeadbeef
[02/14 17:17:55.987]r08: 0xdeadbeef
[02/14 17:17:55.987]r09: 0xdeadbeef
[02/14 17:17:55.987]r10: 0xdeadbeef
[02/14 17:17:56.034]r11: 0xdeadbeef
[02/14 17:17:56.034]r12: 0xfffffffd
[02/14 17:17:56.034] lr: 0x08035901
[02/14 17:17:56.035] pc: 0x08034de2
[02/14 17:17:56.036]hard fault on thread: lcd thread