Getting stack overflow error reboots with a larger char array size

Askingman
Posts: 1
Joined: Sun Feb 24, 2019 10:10 am

Getting stack overflow error reboots with a larger char array size

Postby Askingman » Sun Feb 24, 2019 10:39 am

Hello.

I am new to arduino/esp32 programming and am stuck on a strange (at least to me) issue.

Basically, I have the esp32 running in a soft AP mode with the webServer module running on it and use it to serve basic webpages.

I followed an example tutorial for making the sketch and it works fine, the problems start, when I need to return longer amounts of text. In fact I think the problem has probably nothing to do with the webserver part, but rather with the part of the code where fill the char arrays up with text.

I define a bunch of char arrays like this:

Code: Select all

    char replyHTML[9000];
    char setupForm[5000];
    char sslCert[3000];  
And then fill them up with run of the mill functions like sprintf and preferences.getString.

At first, the ESP32 was just rebooting without any errors, so I reduced the size of the char arrays by a bit and the ESP32 was still rebooting, but this time around it started printing out the following error:

Code: Select all

***ERROR*** A stack overflow in task loopTask has been detected.
abort() was called at PC 0x4008f910 on core 1
So I reduced the size of the char arrays once more and this time, the code worked flawlessly with no reboots.
The problem is, that I that I need to fit about 10000 - 15000 characters combined into the char arrays above.
This can't be a memory problem, because when I upload the code, the editor reports "....leaving 284292 bytes for local variables" and the about 15000 characters I need in total is not much more than 15KB, so there should be plenty of space.

Thank you for any advice.

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: Getting stack overflow error reboots with a larger char array size

Postby ESP_Dazz » Mon Feb 25, 2019 1:35 pm

That's probably because you're declaring these within the void loop() meaning thus goes onto the loopTask's stack which is only around 8K bytes.

Try one of the following:

1) Declaring the arrays as a global

Code: Select all

char replyHTML[9000];
char setupForm[5000];
char sslCert[3000];

void setup() {
    ...
}
void loop() {
    ...
}
2) Declaring the arrays as static within the loopTask

Code: Select all

void loop() {
    static char replyHTML[9000];
    static char setupForm[5000];
    static char sslCert[3000];
}
3) Allocating the arrays on the heap using malloc

Code: Select all

void loop() {
    char *replyHTML = (char *)malloc(sizeof(char) * 9000);
    char *setupForm = (char *)malloc(sizeof(char) * 5000);
    char *sslCert = (char *)malloc(sizeof(char) * 3000);
}

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: Getting stack overflow error reboots with a larger char array size

Postby idahowalker » Mon Feb 25, 2019 6:28 pm

If you are running with freeRTOS taskings then increase the stack size of the task.

put this code at the bottom of the task:

Code: Select all

 
         Serial.print( "fAlarm " );
         Serial.print(uxTaskGetStackHighWaterMark( NULL ));
         Serial.println();
         Serial.flush();
to get an idea of the how the task is doing with the current task size and adjust stack size accordingly.

Who is online

Users browsing this forum: No registered users and 60 guests