xTimerCreate still runs even after Task is deleted

username
Posts: 532
Joined: Thu May 03, 2018 1:18 pm

xTimerCreate still runs even after Task is deleted

Postby username » Thu Sep 21, 2023 7:08 pm

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);

}

MicroController
Posts: 1707
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: xTimerCreate still runs even after Task is deleted

Postby MicroController » Thu Sep 21, 2023 9:23 pm

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.
Last edited by MicroController on Thu Sep 21, 2023 9:48 pm, edited 2 times in total.

username
Posts: 532
Joined: Thu May 03, 2018 1:18 pm

Re: xTimerCreate still runs even after Task is deleted

Postby username » Thu Sep 21, 2023 9:34 pm

Thanks. looks like more reading for me ;-)

Who is online

Users browsing this forum: No registered users and 99 guests