Task Cleanup

jorsborn wrote on Friday, November 02, 2012:

I am trying to understand what happens to a task when it returns (ends). The majority of my tasks are perpetual but I have a few that are ‘one shot’ tasks meaning they may do something, delay, do some more things then return. I noticed the following in one of the demo projects…

#define INCLUDE_vTaskCleanUpResources	0

… but I can’t find this documented anywhere.

Will the idle task clean up the resources used by the task that returns? Does it depend on the option above?

davedoors wrote on Friday, November 02, 2012:

According to http://www.freertos.org/History.txt vTaskCleanUpResources was obsolete and finally removed in V7.0.2.

A task must never return, but can be deleted, so if you want a task to “return” then add vTaskDelay(NULL) to the end of the function that implements it.

When you delete a task the TCB and stack will be freed by the idle task. The stack and TCB were allocated by FreeRTOS so it is right that they are also freed by FreeRTOS. FreeRTOS will not free any memory that the application allocated itself though, that must be cleaned manually.