Skip to main content
Graduate
May 23, 2024
Question

Hardfault when using calloc repetedly in STM32G4

  • May 23, 2024
  • 1 reply
  • 1108 views

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?

    This topic has been closed for replies.

    1 reply

    ST Employee
    May 31, 2024

    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

    Graduate
    June 3, 2024

    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!!!