malloc heap issue with CubeMX Project
after searching more the issue is in sycall.c _sbreak function provided by both CubeMX and ac6 project.
the heap increase check is not adpated to an rtos env (stack is in user ram area sp is not any more the main stack)
The heap break check condition is not perfect
ld file and sbreak should be fixed to compare first it is less than min heap and only if not checking vs stack
so it suceed even in rtos env until min heap is not consumed
ie
caddr_t _sbrk(int incr) {
extern char end asm(“end”);
extern char _min_heap_end asm(“_min_heap_end”);
static char *heap_end;
char *prev_heap_end;
if (heap_end == 0)
heap_end = &end;
prev_heap_end = heap_end;
if (prev_heap_end + incr > &_min_heap_end) {
if (heap_end + incr > stack_ptr) {
// write(1, “Heap and stack collision\n”, 25);
// abort();
errno = ENOMEM;
return (caddr_t) -1;
}
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
LD file changed to
._user_heap_stack :
{
. = ALIGN(4);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
PROVIDE ( _min_heap_end = . );
. = . + _Min_Stack_Size;
. = ALIGN(4);
} >RAM
A work arround it s to do a first malloc and free in main prior to start scheduler to setup the user minilal heap
for the time being it’s what i’ll use as ld file options are overwriten by CubeMX on code generation (a user section would be nice)