Freertos and C++ exceptions

Hello,
We’ve been using Freertos for 1 year for our embedded projects and we really love it.
A bit of context : our main target is a Cortex M33 (LPC55S69) which has plenty of flash and RAM.

We’ve started to port our C code base to C++ and we wonder if we can enable the C++ exceptions safely with the Freertos multithreaded environnement?
We use NXP Mcuxpresso IDE with newlib-none.

I think C++ is (or was) not officially supported by Freertos, but it works very well when C++ exceptions are disabled. However, when we enable it, it seems that the stack unwinding process is not working correctly when an exception is thrown. Sometimes it works, sometimes the terminate handler is called. So if it is supported, what do I have to enable / define to make exceptions thread safe? If it is not, can you point me to another forum / people who know this stuff?

Many thanks
Victor

FreeRTOS has (some) official support for C++, in that the headers wrap all C function declarations in extern “C”, to support it. (I requested this feature over a decade ago, don’t know for sure if it was my request that triggered the support).

As far as exceptions, FreeRTOS itself won’t know about exceptions, and at the very least, you won’t be able to let an exception escape a task. One big question, which is on the C++ implementation to answer, is whether it is thread safe. Sometimes exception processing uses global variables (since it is hard to have stuff on the stack when you are unwinding it) and that will cause an issue if two tasks throw ‘at the same time’. Some unwinding operations may also assume the stack is where it was when the program started, and that will not be true. The exception handling may also use dynamic memory, and you may not have made malloc and free exception safe.

I would say the first place to ask would be whereever there is support for your C++ implementation.

Hi Victor - I believe you also encountered the ARM-GCC bug with concurrent exceptions in multiple tasks crashing? This bug has been seen elsewhere and I’ve written it up here, hopefully comprehensibly:


It will push the fix priority if you would log in above and mark ‘the bug also affects me’.
Thanks,
Best Regards, Dave

Hi Dave,

I think it’s the same bug indeed!
I’ve marked as ‘the bug also affects me’

Best Regards,
Victor