Skip to content

Heap memory leak when calling client.connect() in 2.3.0, 2.4.1, & tip #4733

Closed
@jstuewe

Description

@jstuewe

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: [ESP-12 on Wemos D1 mini]
  • Core Version: [2.3.0, 2.4.1, and latest 5/15/18 code]
  • Development Env: [Arduino IDE under Visual Micro\Visual Studio]
  • Operating System: [Windows]

Settings in IDE

  • Module: [Wemos D1 mini r2]
  • Flash Mode: []
  • Flash Size: [4MB (1M SPIFFS)]
  • lwip Variant: [v2 Lower Memory]
  • Reset Method: []
  • Flash Frequency: [40Mhz]
  • CPU Frequency: [80Mhz]
  • Upload Using: [OTA and SERIAL]
  • Upload Speed: [921600]

Problem Description

Heap goes down every time I use client.connect() in my application.
There is a bug with 2.4.1 core that will do this but I am using 2.3.0 which doesn't have this bug and i've tested several cores.

-No client.write() is necessary. Just connecting causes the issue.
-Loses 184 bytes per connection with core 2.3.0
-Loses 168 bytes per connection with core 2.4.1 and Arduino-master(05152018) tip
-Does NOT loose memory when connecting to 66.35.40.169 or data.sparkfun.com
-Does loose memory when connecting to 172.217.14.174 (google.com), 192.168.1.254 (Uverse residential gateway), or my IOT device
-Moving 'WiFiClient client;' to global or removing 'client = WiFiClient();' does not affect the issue

MCVE Sketch

#include <ESP8266WiFi.h>

const char* ssid = "Stuewe-GN";
const char* password = "putpasswordhere";

//const char* host = "data.sparkfun.com";
//const char* host = "66.35.40.169";

//const char* host = "google.com";
const char* host = "172.217.14.174";

//const char* host = "192.168.1.254";

const int httpPort = 80;
//const int httpPort = 5577; // the port I want to use. It leaks but I switched to port 80 for debugging
const char LedOffCmd[] = "\x71\x24\x0f\xa4\x00\x00\x00\x00\x00\x00";

WiFiClient client;


void setup() {
	Serial.begin(115200);
	delay(10);
	Serial.println();
	Serial.print("Connecting to ");
	Serial.println(ssid);
	WiFi.mode(WIFI_STA);
	WiFi.begin(ssid, password);
	while (WiFi.status() != WL_CONNECTED) {
		delay(500);
		Serial.print(".");
	}
	Serial.println("");
	Serial.print("WiFi connected at IP address: ");
	Serial.println(WiFi.localIP());
}


void loop() {
	delay(3000);

	client = WiFiClient(); // some forums said this might work around 2.4.1 bug

	if (client.connect(host, httpPort)) {
		Serial.print("Connected to ");
		Serial.println(host);
//		client.write(LedOffCmd, 10); // no need to actually write anything
		delay(500);
		while (client.available()) { 
			char c = client.read();
		}

	}
	else {
		Serial.print("Unable to connect to ");
		Serial.println(host);
	}
	client.stop();
	Serial.print("Heap: ");
	Serial.println(ESP.getFreeHeap());
}

Debug Messages

Output with different cores and targets

Architecture Tools: file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/
Include Path file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266
Include Path file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/hardware/esp8266/2.3.0/variants/d1_mini

Connecting to Stuewe-GN
...
WiFi connected at IP address: 192.168.1.26
Connected to data.sparkfun.com
Heap: 47264
Connected to data.sparkfun.com
Heap: 47264
Connected to data.sparkfun.com
Heap: 47264
Connected to data.sparkfun.com
Heap: 47264


Connecting to Stuewe-GN
....
WiFi connected at IP address: 192.168.1.26
Connected to 66.35.40.169
Heap: 47264
Connected to 66.35.40.169
Heap: 47264
Connected to 66.35.40.169 (data.sparkfun.com)
Heap: 47264

Connecting to Stuewe-GN
........
WiFi connected at IP address: 192.168.1.26
Connected to 172.217.14.174
Heap: 47264
Connected to 172.217.14.174
Heap: 47080
Connected to 172.217.14.174
Heap: 46896
Connected to 172.217.14.174
Heap: 46712
Connected to 172.217.14.174
Heap: 46528
Connected to 172.217.14.174
Heap: 46344
Connected to 172.217.14.174 (google.com)
Heap: 46160  (184 bytes)


Connecting to Stuewe-GN
........................
WiFi connected at IP address: 192.168.1.26
Connected to 192.168.1.254 
Heap: 47264
Connected to 192.168.1.254
Heap: 47080  
Connected to 192.168.1.254
Heap: 46896
Connected to 192.168.1.254
Heap: 46712
Connected to 192.168.1.254
Heap: 46528
Connected to 192.168.1.254 (Uverse residential gateway)
Heap: 46344  (184 bytes)


Architecture Tools: file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/
Sketch Book: file:///C:/Users/john/OneDrive/Arduino_ESP
Include Path file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/hardware/esp8266/2.4.1/cores/esp8266
Include Path file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/hardware/esp8266/2.4.1/variants/d1_mini

Connecting to Stuewe-GN
.....................
WiFi connected at IP address: 192.168.1.26
Connected to 66.35.40.169
Heap: 45760
Connected to 66.35.40.169
Heap: 45760
Connected to 66.35.40.169
Heap: 45256
Connected to 66.35.40.169
Heap: 45256
Connected to 66.35.40.169
Heap: 45256
Connected to 66.35.40.169
Heap: 45256
Connected to 66.35.40.169 (data.sparkfun.com)
Heap: 45256

Connecting to Stuewe-GN
.......................
WiFi connected at IP address: 192.168.1.26
Connected to 192.168.1.254
Heap: 45760 
Connected to 192.168.1.254
Heap: 45592
Connected to 192.168.1.254
Heap: 44920
Connected to 192.168.1.254
Heap: 44752
Connected to 192.168.1.254
Heap: 44584
Connected to 192.168.1.254
Heap: 44416
Connected to 192.168.1.254
Heap: 44248
Connected to 192.168.1.254
Heap: 44080
Connected to 192.168.1.254 (Uverse residential gateway)
Heap: 43912  (168 bytes)

Architecture Tools: file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/
Sketch Book: file:///C:/Users/john/OneDrive/Arduino_ESP
Include Path file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/hardware/esp8266/Arduino-master05152018/cores/esp8266
Include Path file:///C:/Users/john/AppData/Local/arduino15/packages/esp8266/hardware/esp8266/Arduino-master05152018/variants/d1_mini

 
 Connecting to Stuewe-GN
..
WiFi connected at IP address: 192.168.1.26
Connected to 66.35.40.169 
Heap: 48872
Connected to 66.35.40.169
Heap: 48872
Connected to 66.35.40.169
Heap: 48368
Connected to 66.35.40.169
Heap: 48368
Connected to 66.35.40.169
Heap: 48368
Connected to 66.35.40.169 (data.sparkfun.com)
Heap: 48368 (No memory leak)


 Connecting to Stuewe-GN
.......................
WiFi connected at IP address: 192.168.1.26
Connected to 172.217.14.174
Heap: 48872
Connected to 172.217.14.174
Heap: 48704
Connected to 172.217.14.174
Heap: 48032
Connected to 172.217.14.174
Heap: 47864
Connected to 172.217.14.174
Heap: 47696
Connected to 172.217.14.174
Heap: 47528
Connected to 172.217.14.174 (google.com)
Heap: 47360   (168 bytes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions