C++ RAII in FreeRTOS

Hello, I am trying to release task allocated resources when I execute vTaskDelete from another task. For this, I wanted to create a RAII object in the task, and free all resources in its destructor. But it looks like the Dtor is not being call when the task is deleted. Is this normal behavior, or do I have a local bug? The task I am deleting has an infinite for loop. So I guess if the task is suspended and then killed, the scope of the function in which the RAII object is defined does not reach its end?

C++ RAII in FreeRTOS

You would have to post the exact source code for us to confirm, but it sounds like the compiler is optimizing out the destructor, under the assumption that you never exit the task’s infinite loop. What I’ve done in cases like this is not directly delete the task, but wake the task up with a message / event that says “kill yourself”, then the task can exit a local scope (and trigger RAII / DTOR) and then commit suicide.

C++ RAII in FreeRTOS

The whole point of using RAII here was not to send a “kill yourself” message to the task 🙂 this is the code: ~~~ class DataRAII { public: DataRAII(DAQ& daq) : daq(daq) {} ~DataRAII() { daq.Stop(); } private: DAQ& daq_; }; void dataProcessingTask(void * argument) { DataRAII raiiguard(daq); daq.Start(); for(;;) { if (xTaskNotifyWait(0x00, 0xFFFFFFFF,……
 }
} The other task would call: vTaskDelete(proctaskhandle); To delete the processing task. ~~~ So if this is the compiler optimizing out the Dtor, shouldnt i be able to fool it by adding a clause with a return statment inside the for loop?

C++ RAII in FreeRTOS

Sorry, I still don’t get it. Maybe I’m wrong but the C-function vTaskDelete has no idea about C++ delete i.e. won’t invoke any DTOR. If you want to power of C++ you have to use C++ consistently and wrap FreeRTOS C-functions accordingly. As a tiny example I’m using a wrapper class dealing with tasks with this DTOR: virtual ~CTask() { if (mhTask) vTaskDelete( mhTask ); }

C++ RAII in FreeRTOS

So with this approach you would have to free all the resources allocated by “mhTask” from the “parent” task and not by “mhTask” itself?

C++ RAII in FreeRTOS

Please realise that vTaskDelete() is a kind of abortion, an exit(). The destructors of objects created in a task will not be called. This behaviour is not a mistake, neither it is “erroneous”. If I ever call vTaskDelete(), it is the task who kills itself, using the parameter NULL. The task knows best what to do to finish its work in a graceful way. vTaskDelete() is not nice, the OS will just eliminate the task from the task-list. If you want a task to stop running, I would recommend sending a messages: “Please stop”. Let the task delete explicitly the objects that it has created. You could define use a sub-scope, that closes before calling vTaskDelete(), like in this example: ~~~ void dataProcessingTask(void * argument) { { DataRAII raiiguard(daq); daq.Start(); for(;;) { if (xTaskNotifyWait(0x00, 0xFFFFFFFF,…… if( hastostop ) { break; } } /* at this point the destructor ~DataRAII() will be called. / } / Kill my self. / vTaskDelete( NULL ); / This line won’t be reached. */ } ~~~

C++ RAII in FreeRTOS

Hi Hein, Thank you very much for the conformation! This is what i suspected. I just really wanted to avoid that extra message.