Page 1 of 1

xTimerCreate still runs even after Task is deleted

Posted: Thu Sep 21, 2023 7:08 pm
by username
Ran into something strange today. I created a xTimerCreate inside a Task. When I delete the task the time continues to run. I expected it to stop. I though with vTaskDelete everything in that task gets wiped, no ?

Code: Select all

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>

TaskHandle_t TaskHandle_Task_1;

/***********************************************************************
 ***********************************************************************/
void vKeepAliveCallback(TimerHandle_t pxTimer)
{
    printf("Keep Alive Update %ld\r\n", (xTaskGetTickCount() * portTICK_PERIOD_MS));
}
/***********************************************************************
 ***********************************************************************/
void Task_1(void *parameter)
{

    TimerHandle_t hSecondTimer;

    printf("Task 1 Started\n");

    hSecondTimer = xTimerCreate("KeepAlive", configTICK_RATE_HZ , pdTRUE, NULL, vKeepAliveCallback); // configTICK_RATE_HZ

    xTimerStart(hSecondTimer, 0);

    for(;;)
    {
        vTaskDelay(200 / portTICK_PERIOD_MS);
    }
}
/***********************************************************************
 ***********************************************************************/
void app_main()
{
    printf("\n***************************\n");
    printf("***   ESP32 System Up   **\n");
    printf("***************************\n");

    xTaskCreatePinnedToCore(
        Task_1,                 // Task function.
        "Task_1",           // name of task.
        2048,                   // Stack size of task
        NULL,                   // parameter of the task
        5,                      // priority of the task
        &TaskHandle_Task_1,     // Task handle to keep track of created task
        1);


    // Let timer run for 5 seconds before we kill Task_1
    vTaskDelay(5000 / portTICK_PERIOD_MS);

    vTaskDelete(TaskHandle_Task_1);

}

Re: xTimerCreate still runs even after Task is deleted

Posted: Thu Sep 21, 2023 9:23 pm
by MicroController
I though with vTaskDelete everything in that task gets wiped, no ?
No. Not at all.
Deleting a task only releases the memory allocated by FreeRTOS for that task's stack and its Task Control Block, nothing more. Or, on a more abstract level, vTaskDelete makes sure that a task will subsequently never get any more CPU time and hence just stops executing code. That's all.
This is one of the major reasons why no task should "delete" another task. A task should only ever delete itself after it has cleaned up/released all resources it owns, especially any and all locks/mutexes it may hold.
xTimerCreate still runs even after Task is deleted
There may be a misconception here. xTimerCreate does what its name suggests: It creates a timer object. The created timer is not associated with and does not "run" inside the task which created it. The timer object exists independently and remains functional until xTimerDelete is called on the timer object from any task.

Re: xTimerCreate still runs even after Task is deleted

Posted: Thu Sep 21, 2023 9:34 pm
by username
Thanks. looks like more reading for me ;-)