I know this has been asked before but I’ve been unable to find an answer, hopefully someone can put me right quickly.
When a task deletes itself by calling vTaskDelete(NULL) and returning, I am aware that the idle task needs to run to clean up any OS resources the task occupied. I have been calling vTaskDelay() from the parent task with a nice long time in it (seconds), with no other tasks running, to let the idle task do the clean-up, however I am still seeing memory being leaked.
I believe what is happening is that memory allocated due to library calls (newlib) made from the task are not being freed. For instance, the first call to malloc() from the new task, say for just four bytes of storage, leads to the heap being reduced by something like1400 bytes, when only four bytes are recovered by the accompanying free(). Subsequent calls to malloc() from the new task behave normally. I see similar heap loss with other library functions called from the new task that require re-entrancy protection (e.g. strtok()).
Obviously the RTOS needs to handle C library re-entrancy (configUSE_NEWLIB_REENTRANT is 1) but what I don’t understand is why I am unable to recover that memory when the task is deleted. Might I be missing a configuration option somewhere? Is my scheme of calling vTaskDelay() actually not working at all and I should be doing something else? Or am I expecting too much?
This is on an STM32F4 processor incorporating the Dave Nadler _sbrk() implementation with FreeRTOS v10.2.1.