Quality RTOS & Embedded Software

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


C++ RAII in FreeRTOS

Posted by evgeny-erlihman on December 6, 2017


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:


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 ]    [ Privacy ]    [ Sitemap ]    [ ]

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

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

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

Xilinx Microblaze and Zynq partner