freertos task does not cleanup c++ class objects on vTaskDelete
Posted: Tue Feb 13, 2024 1:43 am
Hi,
just recently i found a strange issue and im not sure what is the problem:
- freertos,
- esp-idf,
- compiler?
Description:
- when using C++ class local objects inside freertos task, when task is deleted with vTaskDelete the object is not destroyed (destructor is not called) which means there is no full cleanup;
i know its not usual use case and the only explanation is that task function never exit, because after vTaskDelete() there is "void" (i mean like cosmos void) just nothing more.
Here is test example:
This is expected log output, which we can see after uncommenting brackets:
Any thoughts?
just recently i found a strange issue and im not sure what is the problem:
- freertos,
- esp-idf,
- compiler?
Description:
- when using C++ class local objects inside freertos task, when task is deleted with vTaskDelete the object is not destroyed (destructor is not called) which means there is no full cleanup;
i know its not usual use case and the only explanation is that task function never exit, because after vTaskDelete() there is "void" (i mean like cosmos void) just nothing more.
Here is test example:
Code: Select all
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
class TestClass
{
private:
uint8_t _val = 10;
public:
TestClass();
~TestClass();
uint8_t val() {
return _val;
}
};
TestClass::TestClass()
{
printf("constructor\n");
}
TestClass::~TestClass()
{
_val = 0;
printf("destructor\n");
}
static TestClass* obj_p = nullptr;
static void test_task(void* p)
{
// { // uncomment brakets solve problem
TestClass obj;
obj_p = &obj;
obj.val();
vTaskDelay(200);
// }
printf("delete task\n");
vTaskDelete(NULL);
}
extern "C" void app_main()
{
xTaskCreate(test_task, "task", 4000, NULL, 1, NULL);
vTaskDelay(100);
while (obj_p->val())
{
printf("Val: %d\n", obj_p->val());
vTaskDelay(50);
}
printf("end of main\n");
}
Code: Select all
constructor
Val: 10
Val: 10
destructor
delete task
end of main