Skip to content

Heap memory leak caused by client.connect() ? #191

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

Closed
robert-rudolph opened this issue Feb 6, 2020 · 5 comments
Closed

Heap memory leak caused by client.connect() ? #191

robert-rudolph opened this issue Feb 6, 2020 · 5 comments

Comments

@robert-rudolph
Copy link

I call the following function periodically on an ESP32. The free heap decreases each time I call connectToMQTT(), indicating a memory leak.

If I comment out the line containing mqtt_client.connect(), then the memory leak goes away, and I have perfect heap preservation.

I've dug through the library source here and not found any unmatched malloc() calls, so I'm stumped as to where the memory is going. Any help would be appreciated!

void connectToMQTT()
{
  int retries = 0;
  int success = false;
  while ( !success && retries < 10) {
    // mqtt_client.disconnect();
    success = mqtt_client.connect("0031");
    delay(100);
    retries++;
  }
}
@256dpi
Copy link
Owner

256dpi commented Feb 6, 2020

Do you know by how much the heap increases on every call?

@robert-rudolph
Copy link
Author

robert-rudolph commented Feb 6, 2020

I'm loosing between 108 and 836 bytes of heap per call.

this may be a problem with the WiFiClient library for ESP32, and not this library.
Seems some similar leak was present on ESP8266 with PubSubClient library on reconnects:

esp8266/Arduino#4497

esp8266/Arduino#7059

@robert-rudolph
Copy link
Author

Closing, this seems to be a leak in the WiFiClient or WiFiClientSecure libraries within ESP32-Arduino Core. Thank you.

@dmarc1234
Copy link

dmarc1234 commented Jan 5, 2022

I am experiencing the same issue with a failed mqtt_client.connect() on the ESP32, losing between 4700~5000 bytes of heap on each call.

Was this problem ever tracked down ?

@robert-rudolph
Copy link
Author

I traced this to the WifiClient core, it is not an issue in the ardino-mqtt repo.

See this thread for a temp fix: espressif/arduino-esp32#3808 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants