embedded runtime_error ai_generated true

FreeRTOS: Stack overflow detected in task 'MainTask' (high water mark: 0)

ID: embedded/stack-overflow-detected

Also available as: JSON · Markdown
85%Fix Rate
90%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
32 active

Root Cause

RTOS task stack overflowed. Stack too small for call depth, large local arrays, or recursive calls.

generic

Workarounds

  1. 92% success Check high water mark and increase offending task stack
    UBaseType_t hwm = uxTaskGetStackHighWaterMark(NULL); // words remaining. Increase stack by deficit + margin.

    Sources: https://openocd.org/doc/html/

  2. 88% success Move large local arrays to static or heap allocation
    static uint8_t buf[1024]; // instead of stack-allocated. Or use pvPortMalloc().
  3. 85% success Enable configCHECK_FOR_STACK_OVERFLOW=2 for early detection
    #define configCHECK_FOR_STACK_OVERFLOW 2  // checks watermark on every context switch

Dead Ends

Common approaches that don't work:

  1. Double all task stack sizes blindly 72% fail

    Wastes precious SRAM. Identify the offending task and size its stack based on actual usage.

  2. Disable stack overflow checking to hide the error 90% fail

    Stack corruption continues silently, corrupting adjacent task stacks or heap, causing random crashes.