Description
#7384 ## Platform
- Hardware: ESP8266
- Core Version: current version
- Development Env: Platformio
- Operating System: Ubuntu
Settings in IDE
- Module: Wemos D1 mini r2
- Flash Mode: [qio|dio|other]
- Flash Size: 4MB
- lwip Variant: [v1.4|v2 Lower Memory|Higher Bandwidth]
- Reset Method: [ck|nodemcu]
- Flash Frequency: 40Mh]
- CPU Frequency: 80Mhz
- Upload Using: OTA & SERIAL
- Upload Speed: 115200 (serial upload only)
Problem Description
Mermory leak on WiFi use, particularly quickly in case of DNS problems, until the device reboot...
Hi friends and thank you for your work.
After several hours in google searching, I understood that there is no memory leak in the ESP8266WiFi library.
However, the fact remains that the device ends up rebooting, particularly quickly (every mn) when DNS (bind9) problems occur (on cut of internet link for example).
My use : https://github.com/peychart/WiFiPowerStrip/blob/master/Next-version/WiFiManager.cpp, line 109
MCVE Sketch
My project : https://github.com/peychart/WiFiPowerStrip/tree/master/Next-version
So, yes this project isn't very small (between 2k & 18k of free memory)... But, the punishment is the same with the lightweight C version of the prototype (https://github.com/peychart/WiFiPowerStrip) in operation for many months (stable most of the time, except on DNS failure - flashing house lighting)... :-(
Code:
#include <Arduino.h>
#include <ESP8266HTTPUpdateServer.h>
#include <uart.h>
#include "untyped.h" //<-- de/serialization object for JSON communications and backups on SD card
#include "WiFiManager.h" //<-- WiFi connection manager
#include "pins.h" //<-- pins object manager with serial pin extension manager
#include "switches.h" //<-- inputs management
#include "webPage.h" //<-- definition of a web interface
#ifdef DEFAULT_MQTT_BROKER
#include "mqtt.h" //<-- mqtt input/output manager
#endif
#ifdef DEFAULT_NTPSOURCE
#include "ntp.h" //<-- time-setting manager
#endif
#include "setting.h" //<--Can be adjusted according to the project...
#include "debug.h" //<-- telnet and serial debug traces
WiFiManager myWiFi;
...
void setup() {
...
myWiFi.version ( VERSION )
.onConnect ( onWiFiConnect )
.onStaConnect ( onStaConnect )
.ifStaConnected ( ifStaConnected )
.ifConnected ( ifWiFiConnected )
.onStaDisconnect ( onStaDisconnect )
.hostname ( DEFAULTHOSTNAME )
.setOutputPower ( 17.5 )
.restoreFromSD ();
...
}
void loop() {
ESPWebServer.handleClient(); delay(1L); //WEB server
myWiFi.loop(); //WiFi manager
mySwitches.inputEvent(intr, rebound_completed); //Switches management
myPins.timers(); //Timers control for outputs
myPins.serialEvent(); //Serial communication for the serial slave management
#ifdef DEFAULT_MQTT_BROKER
myMqtt.loop(); //MQTT manager
#endif
}
Debug Messages
no msg, device reboot on lack of memory...
FreeMem: 19000
FreeMem: 18808
FreeMem: 18768
FreeMem: 17800
FreeMem: 17736 //<- on every ESPWebServer.send call...
etc...
Workaround (but, not very elegant) :
#ifdef WIFI_MEMORY_LEAKS
struct tcp_pcb;
extern struct tcp_pcb* tcp_tw_pcbs;
extern "C" void tcp_abort (struct tcp_pcb* pcb);
inline void tcpCleanup(){while (tcp_tw_pcbs != NULL) tcp_abort(tcp_tw_pcbs);}
#endif
void ifWiFiConnected() { // run every 30s
#ifdef WIFI_MEMORY_LEAKS
ulong m=ESP.getFreeHeap();
DEBUG_print( F("FreeMem: ") ); DEBUG_print(m); DEBUG_print( F("\n") );
if( m < WIFI_MEMORY_LEAKS ){
ESPWebServer.stop(); ESPWebServer.close(); myWiFi.disconnect(1); //<- auto reconnect...
tcpCleanup();
ESPWebServer.begin();
DEBUG_print(F("TCP cleanup...\n"));
}
#endif
...