Description
This issue was originally named Stack usage in WiFi.onStationModeGotIP too high, probably overflow
Below original report showing my wrong understanding of ESP internals
Basic Infos
- [ X] This issue complies with the issue POLICY doc.
- [ X] I have read the documentation at readthedocs and the issue is not addressed there.
- [ X] I have tested that the issue is present in current master branch (aka latest git).
- [ X] I have searched the issue tracker for a similar issue.
- [ X] If there is a stack dump, I have decoded it.
- [ X] I have filled out all fields below.
Platform
- Hardware: [ESP-12]
- Core Version: [1a44f79]
- Development Env: [VS Micro]
- Operating System: [Windows]
Settings in IDE
- Module: [NodeMCU 1.0 (ESP-12E Module]
- Flash Mode: [qio|dio|other]
- Flash Size: [4MB]
- lwip Variant: [v2 Lower Memory]
- Reset Method: [nodemcu]
- Flash Frequency: [40Mhz]
- CPU Frequency: [80Mhz]
- Upload Using: [SERIAL]
- Upload Speed: [115200]
Problem Description
I was experiencing random crashes in my project which is using ESPAsyncTCP and async-mqtt-library so i decided to check stack usage in event handlers on mqtt library, especially in publish received. Found out that free stack size is below zero. I was getting results like current free stack = -4000
I also checked stack usage in onStationModeGotIP event handler and saw free stack is below zero.
Im not sure if this method of measuring free stack is good enough. In setup method is shows areound 4Kb of free stack which seems to be correct.
Is it a bug? Does 'negative' free stack means that stack overlaps with heap region?
MCVE Sketch
#include <ESP8266WiFi.h>
WiFiEventHandler wifiConnectHandler;
extern "C"
{
#include <cont.h>
extern cont_t* g_pcont;
void DebugFreeStack()
{
register uint32_t *sp asm("a1");
int freestack = 4 * (sp - g_pcont->stack);
Serial.printf("current free stack = %d\n", freestack);
}
}
void connectToWifi()
{
Serial.println("Connecting to Wi-Fi...");
WiFi.begin("xxx", "xxx");
}
void onWifiConnect(const WiFiEventStationModeGotIP& event)
{
Serial.println("Connected to Wi-Fi.");
DebugFreeStack();
}
void setup()
{
Serial.begin(115200);
delay(2000);
Serial.println();
Serial.println();
Serial.printf("version: %s\n", ESP.getFullVersion().c_str());
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
Serial.println("Setup");
DebugFreeStack();
connectToWifi();
}
void loop()
{
}
Debug Messages
version: SDK:2.2.1(cfd48f3)/Core:win-2.5.0-dev/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1-13-g163bb82)/BearSSL:6d1cefc
Setup
current free stack = 4016
Connecting to Wi-Fi...
Connected to Wi-Fi.
current free stack = -240