QUERY · ISSUE
memory malloc error on stm32f407
port-stm32
print error log:
#Malloc: 0x20013560 16
#Malloc: 0x20013570 12
#Malloc: 0x200135A0 16
#Malloc: 0x200135B0 24
**_#Malloc bytes:4096 failed_**
#Testing 1kB...
Addr:0x20013B10
#Testing 1.25kB...
Addr:0x20013B10
#Testing 1.5kB...
Addr:0x20013B10
#Testing 2kB...
**_Addr:0x2001B5B0_**
#Testing 3kB...
###Malloc bytes:3072 failed
source code:
void *m_malloc(size_t num_bytes) {
void *ptr = malloc(num_bytes);
if (ptr == NULL && num_bytes != 0) {
//PrintInfo("num_bytes : %d ", num_bytes);
SEGGER_RTT_printf(0, "#Malloc bytes:%d failed\r\n", num_bytes);
SEGGER_RTT_printf(0, "#Testing 1kB...\r\n");
ptr = malloc(1024);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1024);
} else {
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
free(ptr);
}
SEGGER_RTT_printf(0, "#Testing 1.25kB...\r\n");
ptr = malloc(1250);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1250);
} else {
free(ptr);
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
}
SEGGER_RTT_printf(0, "#Testing 1.5kB...\r\n");
ptr = malloc(1536);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1536);
} else {
free(ptr);
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
}
SEGGER_RTT_printf(0, "#Testing 2kB...\r\n");
ptr = malloc(1900);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1900);
} else {
free(ptr);
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
}
SEGGER_RTT_printf(0, "#Testing 3kB...\r\n");
ptr = malloc(3072);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 3072);
} else {
free(ptr);
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
}
m_malloc_fail(num_bytes);
}
#if MICROPY_MEM_STATS
MP_STATE_MEM(total_bytes_allocated) += num_bytes;
MP_STATE_MEM(current_bytes_allocated) += num_bytes;
UPDATE_PEAK();
#endif
// PrintInfo("malloc %d : %p", num_bytes, ptr);
SEGGER_RTT_printf(0, "#Malloc: 0x%08x %d\r\n", ptr, num_bytes);
return ptr;
}
heap & stack address:
.heap 0x20012f94 0x4000 load address 0x080b2a44
0x20012f94 . = ALIGN (0x4)
0x20016f94 . = (. + _minimum_heap_size)
*fill* 0x20012f94 0x4000
0x20016f94 . = ALIGN (0x4)
.stack 0x20016f94 0x800 load address 0x080b2a44
0x20016f94 . = ALIGN (0x4)
0x20017794 . = (. + _minimum_stack_size)
*fill* 0x20016f94 0x800
0x20017794 . = ALIGN (0x4)
CANDIDATE · ISSUE
Wrong size reported while the memory allocation failed.
bug
Port, board and/or hardware
esp32
MicroPython version
MicroPython v1.20.0-729-g82bf0aa19-dirty on 2024-08-17; M5Coreink with ESP32-PICO-D4
Reproduction
while execute lv display driver create function
self.disp_drv = lv.display_create(self.width, self.height)
width and height is same , equal to 200.
Expected behaviour
expected to print correct allocating bytes.
Observed behaviour
MemoryError: memory allocation failed, allocating 1073659464 bytes
Additional Information
As I search the error message inside repo, I found the exception was throw from m_malloc_fail function, at there, size_t variable was cast to uint, maybe uint is smaller than size_t, so it is overflow.
can we use %zu for size_t instead of cast to uint?
Code of Conduct
Yes, I agree