在ESP32上使用malloc之后,multi_heap_assert
我有此功能将缓冲区转换为base64字符串。
bool SIM7020_ToBase64(const void* p_Buffer, uint16_t Length, std::string* p_Base64)
{
size_t EncodeLength;
unsigned char* OutputBuffer = NULL;
if((p_Buffer == NULL) || (p_Base64 == NULL))
{
return false;
}
// Get the size for memory allocation first.
mbedtls_base64_encode(OutputBuffer, 0, &EncodeLength, (const unsigned char*)p_Buffer, Length);
Here >> OutputBuffer = (unsigned char*)malloc(sizeof(unsigned char) * EncodeLength);
if(OutputBuffer == NULL)
{
ESP_LOGE(TAG, "Can not allocate memory!");
return false;
}
// Encode the data.
mbedtls_base64_encode(OutputBuffer, EncodeLength, &EncodeLength, (const unsigned char*)p_Buffer, Length);
p_Base64->append(std::string((char*)OutputBuffer));
free(OutputBuffer);
return true;
}
但是我得到了以下例外:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4008a92c PS : 0x00060a30 A0 : 0x80084dfc A1 : 0x3ffb1ea0
A2 : 0x3f800000 A3 : 0x00006c44 A4 : 0x00000000 A5 : 0x3ffb23c8
A6 : 0xfffffffc A7 : 0x3ffb23c8 A8 : 0x00000000 A9 : 0xffffffff
A10 : 0x3f800010 A11 : 0xffffffff A12 : 0x3f800014 A13 : 0x00000000
A14 : 0x0000000a A15 : 0xfffffffc SAR : 0x00000000 EXCCAUSE: 0x0000001c
EXCVADDR: 0xffffffff LBEG : 0x4008d76c LEND : 0x4008d77f LCOUNT : 0x00000000
ELF file SHA256: 91b60b0bac67dcc7
Backtrace: 0x4008a929:0x3ffb1ea0 0x40084df9:0x3ffb1ec0 0x40084e47:0x3ffb1ee0 0x4008d2c9:0x3ffb1f00 0x400d2cb9:0x3ffb1f20 0x400d89ee:0x3ffb1f40 0x400d8c7b:0x3ffb2160 0x4008733d:0x3ffb2180
#0 0x4008a929:0x3ffb1ea0 in multi_heap_assert at framework-espidf\components\heap/multi_heap_platform.h:54
(inlined by) multi_heap_malloc_impl at framework-espidf\components\heap/multi_heap.c:431
#1 0x40084df9:0x3ffb1ec0 in heap_caps_malloc at framework-espidf\components\heap/heap_caps.c:111
#2 0x40084e47:0x3ffb1ee0 in heap_caps_malloc_default at framework-espidf\components\heap/heap_caps.c:146
#3 0x4008d2c9:0x3ffb1f00 in malloc at framework-espidf\components\newlib/heap.c:32
#4 0x400d2cb9:0x3ffb1f20 in SIM7020_ToBase64(void const*, unsigned short, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*) at src/SIM7020/Misc/sim7020_tools.cpp:88
那怎么了?我有一个8 MB的PSRAM,并且还启用了RAM:
I (1206) spiram: SPI SRAM memory test OK
I (1206) heap_init: Initializing. RAM available for dynamic allocation:
I (1206) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (1212) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
I (1219) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (1225) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM
I (1231) heap_init: At 3FFC1288 len 0001ED78 (123 KiB): DRAM
I (1237) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (1244) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1250) heap_init: At 4008EBB8 len 00011448 (69 KiB): IRAM
I (1257) cpu_start: Pro cpu start user code
I (1261) spiram: Adding pool of 4096K of external SPI memory to heap allocator
I (1282) spi_flash: detected chip: generic
I (1283) spi_flash: flash io: dio
I (1283) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (1291) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
因此,内存Shoudln´在这里是一个问题。我也不理解错误,即不了解错误的原因。我使用ESP-IDF 4.0.1
。
I have this function to convert a buffer into a base64 string.
bool SIM7020_ToBase64(const void* p_Buffer, uint16_t Length, std::string* p_Base64)
{
size_t EncodeLength;
unsigned char* OutputBuffer = NULL;
if((p_Buffer == NULL) || (p_Base64 == NULL))
{
return false;
}
// Get the size for memory allocation first.
mbedtls_base64_encode(OutputBuffer, 0, &EncodeLength, (const unsigned char*)p_Buffer, Length);
Here >> OutputBuffer = (unsigned char*)malloc(sizeof(unsigned char) * EncodeLength);
if(OutputBuffer == NULL)
{
ESP_LOGE(TAG, "Can not allocate memory!");
return false;
}
// Encode the data.
mbedtls_base64_encode(OutputBuffer, EncodeLength, &EncodeLength, (const unsigned char*)p_Buffer, Length);
p_Base64->append(std::string((char*)OutputBuffer));
free(OutputBuffer);
return true;
}
But I got the following exceptions:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4008a92c PS : 0x00060a30 A0 : 0x80084dfc A1 : 0x3ffb1ea0
A2 : 0x3f800000 A3 : 0x00006c44 A4 : 0x00000000 A5 : 0x3ffb23c8
A6 : 0xfffffffc A7 : 0x3ffb23c8 A8 : 0x00000000 A9 : 0xffffffff
A10 : 0x3f800010 A11 : 0xffffffff A12 : 0x3f800014 A13 : 0x00000000
A14 : 0x0000000a A15 : 0xfffffffc SAR : 0x00000000 EXCCAUSE: 0x0000001c
EXCVADDR: 0xffffffff LBEG : 0x4008d76c LEND : 0x4008d77f LCOUNT : 0x00000000
ELF file SHA256: 91b60b0bac67dcc7
Backtrace: 0x4008a929:0x3ffb1ea0 0x40084df9:0x3ffb1ec0 0x40084e47:0x3ffb1ee0 0x4008d2c9:0x3ffb1f00 0x400d2cb9:0x3ffb1f20 0x400d89ee:0x3ffb1f40 0x400d8c7b:0x3ffb2160 0x4008733d:0x3ffb2180
#0 0x4008a929:0x3ffb1ea0 in multi_heap_assert at framework-espidf\components\heap/multi_heap_platform.h:54
(inlined by) multi_heap_malloc_impl at framework-espidf\components\heap/multi_heap.c:431
#1 0x40084df9:0x3ffb1ec0 in heap_caps_malloc at framework-espidf\components\heap/heap_caps.c:111
#2 0x40084e47:0x3ffb1ee0 in heap_caps_malloc_default at framework-espidf\components\heap/heap_caps.c:146
#3 0x4008d2c9:0x3ffb1f00 in malloc at framework-espidf\components\newlib/heap.c:32
#4 0x400d2cb9:0x3ffb1f20 in SIM7020_ToBase64(void const*, unsigned short, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*) at src/SIM7020/Misc/sim7020_tools.cpp:88
So what is wrong here? I have a 8 MB PSRAM and the RAM is also enabled:
I (1206) spiram: SPI SRAM memory test OK
I (1206) heap_init: Initializing. RAM available for dynamic allocation:
I (1206) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (1212) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
I (1219) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (1225) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM
I (1231) heap_init: At 3FFC1288 len 0001ED78 (123 KiB): DRAM
I (1237) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (1244) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1250) heap_init: At 4008EBB8 len 00011448 (69 KiB): IRAM
I (1257) cpu_start: Pro cpu start user code
I (1261) spiram: Adding pool of 4096K of external SPI memory to heap allocator
I (1282) spi_flash: detected chip: generic
I (1283) spi_flash: flash io: dio
I (1283) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (1291) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
So the memory shoudln´t be a problem here. I also doesn´t understand the error, i. e. doesn´t understand the reason for the error. I use esp-idf 4.0.1
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论