C++ exceptions do not work inside threads

Posts: 5
Joined: Sun Sep 17, 2017 11:52 am

C++ exceptions do not work inside threads

Postby ThorstenS » Fri Nov 10, 2017 2:27 pm


I use ESP-IDF v3.01 (I guess, last commit is from Oct 19th). I enabled C++ exceptions and they work in the main program code. But when creating a thread using xTaskCreate() and with a try-catch block in the thread function, when a C++ exception is thrown, it does not reach the catch block. Instead it comes to a panic.

Code: Select all

0x40087b64: invoke_abort at D:/dev/esp-idf/components/esp32/panic.c:553

0x40087c63: abort at D:/dev/esp-idf/components/esp32/panic.c:553

0x4013550f: __cxxabiv1::__terminate(void (*)()) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc:112

0x40135556: std::terminate() at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc:112

0x401344b3: __cxa_throw at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_throw.cc:87

0x40107d21: ExceptionTestFunc() at d:\dev\msys32\opt\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0/functional:1871

0x40107d46: LightProtocolServerTask(void*) at d:\dev\msys32\opt\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0/functional:1871
LightProtocolServerTask() is my thread function. ExceptionTestFunc() is called by the thread function and throws an exception.

User avatar
Posts: 27
Joined: Thu Sep 28, 2017 9:14 am
Location: California

Re: C++ exceptions do not work inside threads

Postby i_am_mrp » Sun Nov 12, 2017 10:22 am

I think this is a FreeRTOS limitation vs. ESP-IDF (yes FreeRTOS is part of the ESP-IDF distro, see notes below)

FreeRTOS is not C++ aware and will just panic as you saw.

http://www.freertos.org/FreeRTOS_Suppor ... 03495.html

While the preceding link may be old and off ESP-IDF, I think it is still valid as an overall statement of affairs.

There are many extensions, wrappers, etc... that people have built for C++ enabled FreeRTOS but I haven't used any and I don't think ESP-IDF did either.

I am new to the ESP32 and have been using the ESP32 for a couple of months now with C++. I have only enabled exceptions when pulling in some libs that others have wrote (none of that code was using exceptions in FreeRTOS contexts though).

It seems more of us are using ESP-IDF with C++ and probably at some point either Espressif or our Community will tackle it fully.

Posts: 2279
Joined: Sun May 08, 2016 4:11 am

Re: C++ exceptions do not work inside threads

Postby ESP_Angus » Mon Nov 25, 2019 12:03 am

Hi Thorten,

i_am_rp is correct that FreeRTOS by itself doesn't support C++ exceptions in the top-level task function.

ESP-IDF v3.2 added support for automatically wrapping FreeRTOS tasks so they can support C++ exceptions:
https://docs.espressif.com/projects/esp ... on-wrapper

If you update to a more recent IDF and enable this option, this will work as expected. (Also, v3.0.x is End Of Life so suggest updating for two reasons.) ESP-IDF v3.3 is the latest stable release.


Who is online

Users browsing this forum: No registered users and 3 guests