Skip to main content
FedeRico1
Associate II
May 2, 2019
Question

Malloc returns NULL Pointer in FreeRTOS task.

  • May 2, 2019
  • 7 replies
  • 8581 views

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

This topic has been closed for replies.

7 replies

Tesla DeLorean
Guru
May 2, 2019

>>I don't know why this happens.

You've presumably got all the source code, couldn't you look and do some static analysis?

Check if the heap has a mutex, and is thread-safe.

Check if the heap is fragmented, walk the heap in the failure condition and understand the allocations.

Use local/auto variables where possible.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
FedeRico1
FedeRico1Author
Associate II
May 2, 2019

Hi Clive,

Just for my undestanding, does FreeRTOS with heap4 allows malloc?

I alloc memory as first instruction in the task.

Thanks for your time.

Federico

Alex R
Associate II
May 2, 2019

You may want to look into FreeRTOS specific functions to provide memory buffers like Queue and Message:

https://www.freertos.org/a00018.html

https://www.freertos.org/RTOS-message-buffer-API.html

FedeRico1
FedeRico1Author
Associate II
May 2, 2019

I know Messages and Queues but a Middleware that I included into my project requires malloc function.

FedeRico1
FedeRico1Author
Associate II
May 4, 2019

My project is created with cubeMX 5.2.0 and I use FreeRTOS v10.0.1

Now I have a problem with malloc function, inparticular, with this new FreeRTOS version (v10.0.1) if I call malloc function inside a task, the function returns a null pointer. If I call the malloc function in the main.c the function retrun a non null pointer.

I tested the same project with FreeRTOS v9 and the malloc function works also inside a task.

JB2500
Associate II
September 13, 2019

Federico

I have the same problem. I'm using CubeMX V5.3.0 + STM32Cube_FW_F7_V1.15.0 + FreeRTOS V10.0.1.

Did you find a solution?

Essentially my problem is that all memory allocation functions, including malloc, calloc, and pvPortMalloc, return NULL even for small requested sizes e.g. 16. They succeed if called outside any tasks, before any tasks are started.

I've tried heap_1 + heap_3 + heap_4 FreeRTOS heap types.

configSUPPORT_STATIC_ALLOCATION is 0, and configSUPPORT_DYNAMIC_ALLOCATION is 1.

The allocation functions work when I use FreeRTOS V9.0.0 (in STM32Cube_FW_F7_V1.13.0).

Cheers,

JB.

Pavel A.
Super User
September 13, 2019
JB2500
Associate II
September 14, 2019

Thanks!

Excerpt: As of July-2019, Cube-generated projects using FreeRTOS do not properly support malloc/free/etc and family, nor general newlib RTL reentrancy.

JB2500
Associate II
September 16, 2019

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.

Dave Nadler
Senior III
December 18, 2019

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
Explorer II
January 27, 2021

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.