cancel
Showing results for 
Search instead for 
Did you mean: 

Hardfault when using calloc repetedly in STM32G4

newbie_stm32
Associate III

Hi Community,

I recently came across an issue with calloc used inside a FreeRTOS thread where it returns a valid address after initialization. Still, while storing data (out of 67 bytes), it stores only a few of them (around 12 bytes) and hits a hard fault when freeing.

This behavior is not immediately after initializing a thread but after some iterations of calloc followed by free.

Why is that so?

Do you have any suggestions on how to avoid these scenarios and ensure a stable system by utilizing FreeRTOS features?

2 REPLIES 2
Sarra.S
ST Employee

Hello @newbie_stm32

With such limited information, there are multiple hypotheses to think of...

you said the memory allocation was successful, maybe share with us some code snippets of the allocated memory, and the type of data you intend to store? 

>>This behavior is not immediately after initializing a thread but after some iterations of calloc followed by free

each allocation should be paired with exactly one deallocation 

Also, what you can do, is set a watchpoint on the memory address returned by calloc to break execution when the memory is accessed. and always inspect your stack/ heap sizes

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hi @Sarra.S, thanks for your input.

I am parking this discussion here let me get some time to analyze this in my free time. Currently, as a fix I am using FreeRTOS memory management functions (pvPortMalloc followed by vPortFree) without altering the stack allocation. And I have ensured 200+ bytes are still available for the heap with xPortGetFreeHeapSize and the system is stable for now. 

I need to figure out something related to heap allocation with calloc!!!