-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESP8266WebServer uses >2kb of stack when reinitialized in setup() #2557
Comments
@chschu I find this awesome, thank you. I'm an experienced programmer, and I doubt that the stack usage of using a temporary would have occurred to me.
|
@igrr what are your thoughts on this? The issue is real, but I'm not even sure whether I should label it as a bug. |
It's definitely a bug. Not only this can cause a stack overflow, it also causes excessive RAM usage when the web server is allocated statically, even if file uploads are never used. I would suggest changing the type of _currentUpload to std::unique_ptr, and allocating it in Parsing.cpp (where Note, i would also refactor handleClient to eliminate multiple exit paths, many of which do the same thing:
(and now they would also be doing @chschu, i can leave the fix to you if you like. Otherwise let me know and i will prepare a PR. |
@igrr, I'll provide a PR over the weekend. |
@igrr, I just noticed that you probably wanted the whole _currentUpload to be a wrapped inside a std::unique_ptr, while I just did it for |
@chschu Since the PR is merged, can this issue be closed ? |
@d-a-v Yes of course. Thanks for the nudge. |
Basic Infos
Hardware
Hardware: ESP-12F on NodeMCU clone (Geekcreit Doit)
Core Version: 2.3.0
Description
When reassigning a ESP8266WebServer in the
setup()
function, e.g. to make it listen only on the AP IP address, a temporary instance is created on the stack. This requires more than 2 kilobytes of stack space, and is pretty likely to overrun the stack boundary at 4 kilobytes in complex setups. This may or may not be detected bycont_check
, and lead to strange errors and WDT resets.The reason for this huge stack allocation is the 2K buffer in HTTPUpload contained in a ESP8266WebServer instance.
Strictly speaking, this is NOT a bug, and the stack is properly released after
setup()
returns. Still, it is somewhat inconvenient considering the small total stack size. The code below illustrates some workarounds (CASE 2 and 3) using more complex C++ syntax, but it would be nicer to not have to write things like that.Settings in IDE
Module: NodeMCU 1.0 (ESP-12E Module)
Flash Size: 4MB (3M SPIFFS)
CPU Frequency: 80Mhz
Upload Using: SERIAL
Sketch
Debug Messages
CASE 0: no reinitialization
CASE 1: reinitialize from an implicit temporary instance created on the stack
CASE 2: reinitialize from an explicit temporary instance created on the heap
CASE 3: reinitialize with "placement new" (no temporary instance)
The value of "unmodified stack" varies a bit even for the same CASE, which may be caused by some uninitialized variables. The value of "current free stack" is always the same for one CASE, as one would expect.
The text was updated successfully, but these errors were encountered: