cancel
Showing results for 
Search instead for 
Did you mean: 

Malloc returns NULL Pointer in FreeRTOS task.

FedeRico1
Associate II

Hello Community,

I'm using an ST Nucleo-F767ZI and I create my project, for Atollic True Studio, with Cube32MX.

In my project I included FreeRTOS v10.

I'm in troubled with the malloc function, because this function return a NULL pointer.

I have several tasks, and I'm not able to allocate memory inside a task.

If I invoke the malloc function in the main.c, the function returns a pointer.

I don't know why this happens.

Thanks for the help.

Federico

14 REPLIES 14
JB2500
Associate III

I've just tried creating an STM32F767VI FreeRTOS project with the STM32CubeMX in STM32CubeIDE (V1.0.2) rather than the standalone STM32CubeMX (V5.3.0). With this project the offending _sbrk function is in sysmem.c instead of syscalls.c.

Memory allocation in FreeRTOS tasks still fails with this function as is, and works with it renamed / deleted.

I still don't know whether the default _sbrk function (the one that gets used if the sysmem.c / syscalls.c one is renamed / deleted) along with heap_4.c is satisfactory, or whether a solution such as Dave Nadler's one (heap_useNewlib.c) is required.

ST, please advise.

Thank you, very useful info.

> I don't understand why the "<_sbrk>" is in such a bizarre place.

This looks like just an artifact of disassembly.

-- pa

Seems like the exact issue. This saved us lot of time.

If you expect malloc to work with FreeRTOS, use the code and options described on my web page:

http://www.nadler.com/embedded/newlibAndFreeRTOS.html

FMagr.1
Associate

Hi guys,

Maybe it can help someone with the same issue.

Just using

pvPortMalloc(size);

and

vPortFree(ptr);

works inside a thread with Heap 4 scheme.

Please read more at https://embeddedartistry.com/blog/2018/01/15/implementing-malloc-with-freertos/ .

Thank you.