Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

C++ RAII in FreeRTOS

Posted by evgeny-erlihman on December 6, 2017

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

Posted by waveringradiant on December 6, 2017

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

Posted by evgeny-erlihman on December 7, 2017

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 raii_guard(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

Posted by hs2sf on December 7, 2017

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

Posted by evgeny-erlihman on December 7, 2017

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

Posted by heinbali01 on December 7, 2017

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

Posted by evgeny-erlihman on December 7, 2017

Hi Hein,

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


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Meet us at Embedded World. Hall 3A-525.

Hear from Richard Barry at Embedded World. Feb 28, 16:00, Hall 4-428.

Video: Watch James Gosling & Richard Barry at re:Invent, Las Vegas 2017.

FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS