esp8266 / esp8266/Arduino

Implement Esp.getFreeSysStack

Open
#5,148 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

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](https://github.com/esp8266/Arduino/blob/master/POLICY.md).
- [ X] I have read the documentation at [readthedocs](https://arduino-esp8266.readthedocs.io/en/latest) 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](https://stackoverflow.com/help/mcve) Sketch

```cpp
#include
WiFiEventHandler wifiConnectHandler;

extern "C"
{
#include
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

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.