Free memory does not return to its previous state

My two cents:

It’s very frustrating as a newbie to get stuck into something that (apparently) makes no sense. However, one cannot try to tackle a complex project (your logic + FreeRTOS) and expect that things works nicely since the very first time.

FreeRTOS, as any other RTOS, is a complex software full of concepts and particular implementations for any of such concepts. Using dynamic allocated memory isn’t as nice as in the PC, so if you need or want to use it, then you need to understand how it works, from the client’s point view down to how the linker and .ld files works. It can be a daunting task.

FreeRTOS has a feature that I didn’t see anyone mention in the thread: when you destroy a dynamic memory allocated task (or any other object), the memory isn’t returned immediatly. Memory is returned only when the IdleTask runs, so you application must let this task to execute, otherwise memory won’t be returned. From the official documentation:

NOTE: The idle task is responsible for freeing the RTOS kernel allocated memory from tasks that have been deleted. It is therefore important that the idle task is not starved of microcontroller processing time if your application makes any calls to vTaskDelete (). Memory allocated by the task code is not automatically freed, and should be freed before the task is deleted.

As anyone has stated, avoid using dynamic allocated memory. If you can’t, as it seems, you should write your own malloc() and free() functions if none of the 5 different FreeRTOS memory schemes solves your problem. Or write your own memory pool that is able to avoid fragmentation (kind of union in the C language).