Unhandled exception happens only in a specific point of the code

ESP32, I wrote these functions:

void Display::PrintMessage(String message, bool clear, int offset)
{
    PrintMessage(message.c_str(), clear, offset);
}

void Display::PrintMessage(const char *message, bool clear, const uint8_t *font, int height, int offset)
{
    if (strlen(message) == 0) return;
    if (clear) _u8g2.clearBuffer();
    _u8g2.setFont(font);    
    int w = _u8g2.getStrWidth(message);
    int x = (_u8g2.getDisplayWidth() - w) / 2;
    int y = _u8g2.getDisplayHeight() / 2 + offset;
    _u8g2.setDrawColor(0);
    _u8g2.drawBox(0, y - height, _u8g2.getDisplayWidth(), height);
    _u8g2.setDrawColor(1);
    _u8g2.drawStr(x, y, message);
    _u8g2.sendBuffer();
}

String Languages::GetText(int index)
{
    if (index < _listTranslations.size()) return _listTranslations.get(index);
    return String();
}

where:

MyLinkedList::LinkedList<String> _listTranslations;

Example of usage:

void goHome()
{
  _display.PrintMessage(_languages.GetText(LANG_DISPLAY_HOMING));
  state = States::Homing;
}

I have a lot of similar functions in my code and they work as expected.
Just one of these callings leads to the following exception:

Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (loopTask)
Core  1 register dump:
PC      : 0x4010269e  PS      : 0x00060236  A0      : 0x8010284b  A1      : 0x3ffd3700  
A2      : 0x3ffd37ac  A3      : 0x00000006  A4      : 0x3ffc8658  A5      : 0x00000000  
A6      : 0x3ffc8658  A7      : 0xff000000  A8      : 0xa381ee39  A9      : 0x3ffd3860  
A10     : 0x3ffc532c  A11     : 0x3ffd38ac  A12     : 0x00000001  A13     : 0x00000000  
A14     : 0x3ffc8658  A15     : 0xff000000  SAR     : 0x0000000a  EXCCAUSE: 0x00000001  
EXCVADDR: 0x00000000  LBEG    : 0x40092405  LEND    : 0x40092415  LCOUNT  : 0xfffffffe  

Backtrace: 0x4010269b:0x3ffd3700 0x40102848:0x3ffd3740 0x4010288b:0x3ffd3760 0x40102901:0x3ffd3780 0x400d7dea:0x3ffd37a0 0x400d7df5:0x3ffd37e0 0x400d7df5:0x3ffd3820 0x400d7df5:0x3ffd3860 0x400d7df5:0x3ffd38a0 0x400d7df5:0x3ffd38e0 0x400d7df5:0x3ffd3920 0x400d7df5:0x3ffd3960 0x400d7df5:0x3ffd39a0 0x400d7df5:0x3ffd39e0 0x400d7df5:0x3ffd3a20 0x400d7df5:0x3ffd3a60 0x400d7df5:0x3ffd3aa0 0x400d7df5:0x3ffd3ae0 0x400d7df5:0x3ffd3b20 0x400d7df5:0x3ffd3b60 0x400d7df5:0x3ffd3ba0 0x400d7df5:0x3ffd3be0 0x400d7df5:0x3ffd3c20 0x400d7df5:0x3ffd3c60 0x400d7df5:0x3ffd3ca0 0x400d7df5:0x3ffd3ce0 0x400d7df5:0x3ffd3d20 0x400d7df5:0x3ffd3d60 0x400d7df5:0x3ffd3da0 0x400d7df5:0x3ffd3de0 0x400d7df5:0x3ffd3e20 0x400d7df5:0x3ffd3e60 0x400d7df5:0x3ffd3ea0 0x400d7df5:0x3ffd3ee0 0x400d7df5:0x3ffd3f20 0x400d7df5:0x3ffd3f60 0x400d7df5:0x3ffd3fa0 0x400d7df5:0x3ffd3fe0 0x400d7df5:0x3ffd4020 0x400d7df5:0x3ffd4060 0x400d7df5:0x3ffd40a0 0x400d7df5:0x3ffd40e0 0x400d7df5:0x3ffd4120 0x400d7df5:0x3ffd4160 0x400d7df5:0x3ffd41a0 0x400d7df5:0x3ffd41e0 0x400d7df5:0x3ffd4220 0x400d7df5:0x3ffd4260 0x400d7df5:0x3ffd42a0 0x400d7df5:0x3ffd42e0 0x400d7df5:0x3ffd4320 0x400d7df5:0x3ffd4360 0x400d7df5:0x3ffd43a0 0x400d7df5:0x3ffd43e0 0x400d7df5:0x3ffd4420 0x400d7df5:0x3ffd4460 0x400d7df5:0x3ffd44a0 0x400d7df5:0x3ffd44e0 0x400d7df5:0x3ffd4520 0x400d7df5:0x3ffd4560 0x400d7df5:0x3ffd45a0 0x400d7df5:0x3ffd45e0 0x400d7df5:0x3ffd4620 0x400d7df5:0x3ffd4660 0x400d7df5:0x3ffd46a0 0x400d7df5:0x3ffd46e0 0x400d7df5:0x3ffd4720 0x400d7df5:0x3ffd4760 0x400d7df5:0x3ffd47a0 0x400d7df5:0x3ffd47e0 0x400d7df5:0x3ffd4820 0x400d7df5:0x3ffd4860 0x400d7df5:0x3ffd48a0 0x400d7df5:0x3ffd48e0 0x400d7df5:0x3ffd4920 0x400d7df5:0x3ffd4960 0x400d7df5:0x3ffd49a0 0x400d7df5:0x3ffd49e0 0x400d7df5:0x3ffd4a20 0x400d7df5:0x3ffd4a60 0x400d7df5:0x3ffd4aa0 0x400d7df5:0x3ffd4ae0 0x400d7df5:0x3ffd4b20 0x400d7df5:0x3ffd4b60 0x400d7df5:0x3ffd4ba0 0x400d7df5:0x3ffd4be0 0x400d7df5:0x3ffd4c20 0x400d7df5:0x3ffd4c60 0x400d7df5:0x3ffd4ca0 0x400d7df5:0x3ffd4ce0 0x400d7df5:0x3ffd4d20 0x400d7df5:0x3ffd4d60 0x400d7df5:0x3ffd4da0 0x400d7df5:0x3ffd4de0 0x400d7df5:0x3ffd4e20 0x400d7df5:0x3ffd4e60 0x400d7df5:0x3ffd4ea0 0x400d7df5:0x3ffd4ee0 0x400d7df5:0x3ffd4f20 0x400d7df5:0x3ffd4f60 0x400d7df5:0x3ffd4fa0 |<-CONTINUES

I printed out the heap size and stack watermark just before the call:

Serial.println(esp_get_free_heap_size());
Serial.println(uxTaskGetStackHighWaterMark(NULL));

160156
5856

Changing a bit the code, for example:

_display.PrintMessage("HOMING");
_languages.GetText(LANG_DISPLAY_HOMING); // useless, but just to test the function

leads to a normal behavior, i.e. no reboot.

It's the first time I'm facing such an issue. I tried to read here and there on the web but I found few hints about increasing the stack size, but before going further in this direction I would like to ask here what should I do to find out the root cause of the exception.

There are no variations of PrintMessage in your posted code that take a single parameter.

https://snippets-r-us.com/
...and...
https://www.youtube.com/watch?v=tdQL_7osGzc
...come to mind.

It seems that the issue is related to the call to ``_display.PrintMessage(_languages.GetText(LANG_DISPLAY_HOMING)); .
Double-check this function and its parameters.
or increase stack size

build_flags = -D CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER="taskSTACK_GROWS_DOWN 1"

I have default parameters in the header:

void PrintMessage(String message, bool clear=true, int offset=0);
void PrintMessage(const char *message, bool clear=true, const uint8_t *font=u8g2_font_8x13_tf, int height=12, int offset=0);

If it was a syntax error it would catch it at compile time, I guess.
I'm afraid is something related the the memory, since calling the above lines in other parts of the code (dozen of times) works fine.

I'm trying to increase the stack size right now. By the way, is there a way to know the stack usage at runtime (like for the heap) or to estimate the maximum call depth at compile time?

Adding this build_flags changed nothing, unfortunately.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.