Stack issue

MichaelOToole
Posts: 4
Joined: Mon Sep 11, 2023 11:57 am

Stack issue

Postby MichaelOToole » Mon Sep 11, 2023 12:53 pm

In a recent project I noticed my board resets after 18 touch events/processes.

Basically I use Makerfabs-ESP32-S3-SPI-TFT-with-Touch board and a mixed bag of example code to produce a Macro Keyboard.
It works perfectly for about 18 button(touch) presses but then resets (I run out of Stack). Chances are it's my code but...

I found example code to show how much stack was used/remained. I put the code before and after suspect function call to print Stack used for that process. For example, the first call to load and display a menu/bmp uses over 1k, each call thereafter uses about an additional 200 bytes... nothing appeared to pop the data off the stack...

Does the compiler pop after a function call or does the code need to do the house keeping?
How would I examine the stack to see what was pushed to it? (knowing this might point me in the right direction)...

Any help appreciated....
Mike

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Stack issue

Postby ESP_Sprite » Tue Sep 12, 2023 3:53 am

MichaelOToole wrote:
Mon Sep 11, 2023 12:53 pm
I found example code to show how much stack was used/remained.
Are you sure that code doesn't simply show the stack high point (that is, the maximum of stack ever used?)

MichaelOToole
Posts: 4
Joined: Mon Sep 11, 2023 11:57 am

Re: Stack issue

Postby MichaelOToole » Tue Sep 12, 2023 4:11 am

I have changed it a little, here's the code...

Code: Select all

void printStack(char *mytxt)
{
  char *SpStart = NULL;
  char *StackPtrAtStart = (char *)&SpStart;
  
  UBaseType_t watermarkStart = uxTaskGetStackHighWaterMark(NULL);
  
  char *StackPtrEnd = StackPtrAtStart - watermarkStart;
  
  if(stacktot == 0) { stackori = stacktot = (uint32_t)StackPtrAtStart - (uint32_t)StackPtrEnd; } 
  stackrun += (uint32_t)stacktot - ((uint32_t)StackPtrAtStart - (uint32_t)StackPtrEnd);
  
  Serial.printf("Free Stack near previous: %d, now: %d,", stackori, (uint32_t)StackPtrAtStart - (uint32_t)StackPtrEnd);
  Serial.printf(" this loop [%d] used: %4d, total stack used:%4d (%s)\r\n", loopcount++, (uint32_t)stacktot - ((uint32_t)StackPtrAtStart - (uint32_t)StackPtrEnd), stackrun, mytxt);
  
  stacktot = (uint32_t)StackPtrAtStart - (uint32_t)StackPtrEnd;
}
I didn't check what the code actually reports, I just assumed as if uses StackPtrEnd = StackPtrAtStart - watermarkStart...
I best take a closer look...

The full code can be seen here: https://github.com/phpbbireland/3x5_tou ... stCode.ino

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Stack issue

Postby ESP_Sprite » Tue Sep 12, 2023 5:21 am

uxTaskGetStackHighWaterMark indeed returns the largest amount of stack the task ever used, at this moment or anywhere in the past. So you would not expect the number that this returns ever to go down.

MicroController
Posts: 1707
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Stack issue

Postby MicroController » Tue Sep 12, 2023 6:13 am

each call thereafter uses about an additional 200 bytes... nothing appeared to pop the data off the stack...
The problem is that you have an infinite recursion in your code:
setFileNames -> processMenu -> processMenuLine -> setFileNames -> ...

MichaelOToole
Posts: 4
Joined: Mon Sep 11, 2023 11:57 am

Re: Stack issue

Postby MichaelOToole » Tue Sep 12, 2023 9:58 am

To be honest, I did suspect something on those lines, it makes perfect sense but, I haven't coded in years and had convinced myself is was something to do with string manipulation, or my lack of knowledge of modern coding techniques, or the Cat...

Many thanks, I will fix it...
Mike

MichaelOToole
Posts: 4
Joined: Mon Sep 11, 2023 11:57 am

Re: Stack issue

Postby MichaelOToole » Tue Sep 12, 2023 1:43 pm

All fixed, many thanks MicroController...
Now to make it useful... :mrgreen:

MicroController
Posts: 1707
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Stack issue

Postby MicroController » Wed Sep 13, 2023 4:01 pm

Good luck with your project :)

Who is online

Users browsing this forum: No registered users and 91 guests