PubSubClient memory leak issue?

Hi All

ESP8266
Arduino Core 2.5.2
PubSubClient 2.7.0

I have an issue with PubSubClient library over SSL

I noticed that the library uses 1KB aprox of heap every time it calls connect()

Every time my router looses internet connection, pubSubClient.connected() is called and then it burns ~1KB of heap, every time, without releasing the OLD memoty

I tested it using Smartphone HotSpot, disabling the MOBILE DTATA (just MOBILE DATA) wehn ESP is connected

Here is a saimple sketch that reproduces the issue

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "********";
const char* password = "***********";

const char* mqttServer = "***********";
const int mqttPort = 27468;
const char* mqttUser = "********";
const char* mqttPassword = "***********";

const char* ClientID = "ESP8266TEST";

WiFiClientSecure espClient;
PubSubClient client(espClient);

String GetStatus()
{
	return
		"ESP.getFreeContStack is " + String(ESP.getFreeContStack()) +
		", ESP.getFreeHeap is " + String(ESP.getFreeHeap()) +
		", WiFi.SSID is " + String(WiFi.SSID());
}

void setup_wifi() {

	delay(10);

	Serial.println();
	Serial.print("Connecting to ");
	Serial.println(ssid);

	WiFi.begin(ssid, password);

	while (WiFi.status() != WL_CONNECTED) {
		delay(500);
		Serial.print(".");
	}

	Serial.println("");
	Serial.println("WiFi connected");
	Serial.println("IP address: ");
	Serial.println(WiFi.localIP());
}

void reconnect() {


	Serial.println("reconnect()");

	while (!client.connected()) {
		Serial.print("Attempting MQTT connection...");
		Serial.println(GetStatus() + "...   ");
		// Attempt to connect
		if (client.connect(ClientID, mqttUser, mqttPassword)) {
			Serial.println("connected");
		}
		else {
			Serial.print("failed, rc=");
			Serial.print(client.state());
			Serial.println(" try again in 1 seconds");
			delay(1000);
		}
	}
}


void setup() {
	pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
	Serial.begin(115200);
	setup_wifi();

	espClient.setInsecure();
	client.setServer(mqttServer, mqttPort);
}

int loopcount = 0;

void loop() {

	if (!client.connected()) {
		reconnect();
	}

	client.loop();

	if (++loopcount % 10000 == 0)
		Serial.println(GetStatus() + "...   ");
}

And here is what is serial printed

ESP.getFreeContStack is 1520, ESP.getFreeHeap is 19688, WiFi.SSID is AndroidHP...
...
• DISABLE MOBILE DTATA
...
ESP.getFreeContStack is 1520, ESP.getFreeHeap is 39720, WiFi.SSID is AndroidHP...
...
• ENABLE MOBILE DTATA
...
reconnect()
Attempting MQTT connection...ESP.getFreeContStack is 1520, ESP.getFreeHeap is 39720, WiFi.SSID is AndroidHP...
connected
ESP.getFreeContStack is 1520, ESP.getFreeHeap is 18632, WiFi.SSID is AndroidHP...
...
• DISABLE MOBILE DTATA
...
ESP.getFreeContStack is 1520, ESP.getFreeHeap is 38664, WiFi.SSID is AndroidHP...
...
• ENABLE MOBILE DTATA
...
reconnect()
Attempting MQTT connection...ESP.getFreeContStack is 1520, ESP.getFreeHeap is 38664, WiFi.SSID is AndroidHP...
connected
...
ESP.getFreeContStack is 1520, ESP.getFreeHeap is 17576, WiFi.SSID is AndroidHP...
...

So It seems to me that it burns 1KB aprox every time

What I'm doing wrong?

Most probably this isn't related to PubSubClient but a leak in the ESP8266 core. Did you the same test with a non-SSL client?

Hi, no I didn't try... Il try next week

BTW, any idea on how to investigate and solve the issue?

What do you suggest?

Hi, just tested, I can confirm you have the same issue with a non-SSL client

same memory leak

mattdev:
BTW, any idea on how to investigate and solve the issue?

Connect to your PC over USB and use Serial.print statements at various points in the code to print heap space and where in the code your at.
This should help confirm the place where memory is not being released as expected.
If it is in the PubSubClient then check its github issues and report the problem if it is not already present.

Riva:
Connect to your PC over USB and use Serial.print statements at various points in the code to print heap space and where in the code your at.
This should help confirm the place where memory is not being released as expected.
If it is in the PubSubClient then check its github issues and report the problem if it is not already present.

Hi thank you

BTW this is exaclty what i did...

you can see the logs in the original message

The memory leak is when pubsubclient calls connect()
it burns 1kb heap every call...

any idea on how to investigate more?

Have you checked the github issues? Currently the first item is regarding memory leak. Assuming your not making the same mistakes the OP was then maybe add to the issue.

Riva:
Have you checked the github issues? Currently the first item is regarding memory leak. Assuming your not making the same mistakes the OP was then maybe add to the issue.

Hi as stated we open the same issue on the github page
we moved to this forum since it seems more specific to the WiFICLient...
see for example

https://github.com/esp8266/Arduino/issues/4497

can anybody help to investigate deeper?

mattdev:
Hi as stated we open the same issue on the github page
we moved to this forum since it seems more specific to the WiFICLient...

I must have missed where you said the issue has already been posted on github as you have not mentioned it in this thread. Also your now saying the issue is with the WifiClient and not PubSubClient.
Maybe someone else is willing to step in and help.

2.4.1 memory leak · Issue #4497 · esp8266/Arduino · GitHub

can anybody help to investigate deeper?

That issue was fixed in 2.4.2. So it must be something different.