Quality RTOS & Embedded Software

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


vApplicationStackOverflowHook cannot wake ...

Posted by Martin Velek on November 14, 2011

please, could someone help me, what I am doing wrong? I have created two tasks, the first is used to generate stack overflow the latter to detect it. In the function vApplicationStackOverflowHook, I am trying to send something (even uninitialized, does not matter) to the queue. The variable pxHigherPriorityTaskWoken is pdTRUE and portYIELD_FROM_ISR() is called. However the vApplicationStackOverflowHook is called again and again. How should I end the vApplicationStackOverflowHook to wake up the function detect_stack_error?

Thank you for your advices.


ARM7 - LPC2478 - v7.0.2

xQueueHandle xQueue;
void stack_error( void * pvParameters ) // priority normal
uint32_t stack[40];

for (int i = 0; i < 50; ++i)
stack = i;

while (1)

void detect_stack_error( void * pvParameters ) // priority highest
xQueue = xQueueCreate(10, sizeof(unsigned char));
unsigned char a;
while (1)
xQueueReceive(xQueue, &a, portMAX_DELAY);
__asm ("NOP\n");

void vApplicationStackOverflowHook(void *pxTask, char *pcTaskName)

portBASE_TYPE pxHigherPriorityTaskWoken;
unsigned char pvItemToQueue;
xQueueSendFromISR( xQueue, & pvItemToQueue, &pxHigherPriorityTaskWoken );


RE: vApplicationStackOverflowHook cannot wake ...

Posted by Richard Damon on November 14, 2011
If you are not running on a system with an MMU, once you have experienced a stack overflow, the system may well be now in an unstable state and not able to continue.

I am also not sure what parts of the API are allowed to be called from the stack overflow routine, and I suspect trying to force a task switch may not be allowed there, as the stack check is happening within the task switch routine.

Normally the overflow hook will be code to somehow output an error message somewhere if possible with primitive code, and then restart the processor.

RE: vApplicationStackOverflowHook cannot wake ...

Posted by Richard on November 14, 2011
Generally, unless you are using an MPU port, you cannot recover from a stack overflow. When you have an MPU, you get notification of the stack overflow before any corruption happens as a trap is triggered as soon as a write outside of the region allocated to the stack is attempted. Most ports don't use an MPU, and you only know a stack overflow has happened some time after it has happened - and at that point you don't know what the system state is so all you know is you know nothing. Generally a stack overflow will corrupt a TCB, so the task you are trying to yield from is already corrupt. Even if you are lucky enough that something sensible happens, you will just re-enter the context switch and detect the stack overflow again.


Also consider that the stack overflow is called from the context switch mechanism. You cannot attempt to re-enter the context switch by requesting a context switch when you are already in a context switch (if that makes any sense at all ;o).

RE: vApplicationStackOverflowHook cannot wake ...

Posted by Yuantu Huang on October 25, 2012
Hi Richard,

Do you mean to use xQueueSend or xQueueSendFromISR all OK in the stack overflow hook function?

void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed portCHAR *pcTaskName)
// do not call portYIELD_FROM_ISR();


void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed portCHAR *pcTaskName)


RE: vApplicationStackOverflowHook cannot wake ...

Posted by Richard Damon on October 26, 2012
The context that FreeRTOS calls the vApplicationStackOverflowHook is not suitable for making calls to most of the FreeRTOS API as you are in the middle of the scheduler.

As I said before, at the point where you have detected that you have overflowed the stack, you are really in a state where you can not trust your system, so trying to continue is really not advised.

RE: vApplicationStackOverflowHook cannot wake ...

Posted by Yuantu Huang on October 28, 2012

Thank you very much. That is the reason why pcTaskName can not be printed sometimes when overflow happens just because no stack is available for the task? As you suggested, could things to do in vApplicationStackOverflowHook be flashing LED or loop forever?

RE: vApplicationStackOverflowHook cannot wake ...

Posted by Richard Damon on October 28, 2012
Yes. At the point that it has detected the problem, there is significant chance that your system memory is corrupted. Any code there should not depend on the rest of the system being operational. An infinite loop is one reasonable process (possible blinking an led also), or rebooting the system. The reboot can possible pass on the information about why the reboot was caused and the new invocation of the system may be able to send out a message about it, hopefully before the problem reoccurs.

[ 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.

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