Why vTaskDelete(NULL) does not actually delete task?
Posted: Tue Jul 31, 2018 9:30 am
Hi everybody!
I've noticed a problem when using vTaskDelete(NULL). This is the last statement in task function and I expect it to delete task itself and release resources allocated for task. But unfortunately I see that task state changes from eBlocked/eRunning state to eReady state.
Here is a little snippet of code, illustrating this problem:
This is not a problem when you start tasks which are running during application lifetime, but I use the approach with worker tasks executing for lengthy operations. And each subsequent worker task runs twice slower than previous one (for example, first task takes 10 seconds for completion, second - 20 seconds, third - 40 seconds, forth - 80 seconds and so on). I suppose this happens because of orphaned tasks, which are waiting for something...
I've noticed a problem when using vTaskDelete(NULL). This is the last statement in task function and I expect it to delete task itself and release resources allocated for task. But unfortunately I see that task state changes from eBlocked/eRunning state to eReady state.
Here is a little snippet of code, illustrating this problem:
Code: Select all
#define SLEEP(ms) vTaskDelay(ms / portTICK_PERIOD_MS)
...
TaskHandle_t handle = NULL;
xTaskCreate(&testTask, "TestTask", 4096, NULL, 10, &handle);
ESP_LOGE(TAG, "Test task handle is 0x%.8X", handle);
while (1)
{
volatile eTaskState state = eTaskGetState(handle);
ESP_LOGI(TAG, "Task 0x%.8X state is %u", handle, state); // State is 2 when task is running (that's expected,
// cause task is sleeping most of time
// But when task is completed, state is changed to 1
// and remains 1 forever
SLEEP(500);
}
...
void testTask(void *pvParameters)
{
ESP_LOGW(TAG, "Test task started");
for (uint8_t i = 0; i < 60; i++)
{
SLEEP(1000);
ESP_LOGW(TAG, "Test task iteration %u", i);
}
ESP_LOGW(TAG, "Test task ended");
vTaskDelete(NULL);
}