From 042ceb904c57217b5d17b4704dd503ce141778af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 17 Feb 2017 23:40:41 +0100 Subject: [PATCH] Fix memory leak in MQTT connection method --- code/espurna/mqtt.ino | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 23b3e458..33a6ad2a 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -139,8 +139,7 @@ void mqttConnect() { if (!mqtt.connected()) { - char * host = strdup(getSetting("mqttServer", MQTT_SERVER).c_str()); - if (strlen(host) == 0) return; + if (getSetting("mqttServer", MQTT_SERVER).length() == 0) return; // Last option: reconnect to wifi after MQTT_MAX_TRIES attemps in a row #if MQTT_MAX_TRIES > 0 @@ -161,6 +160,7 @@ void mqttConnect() { mqtt.disconnect(); + char * host = strdup(getSetting("mqttServer", MQTT_SERVER).c_str()); unsigned int port = getSetting("mqttPort", MQTT_PORT).toInt(); char * user = strdup(getSetting("mqttUser").c_str()); char * pass = strdup(getSetting("mqttPassword").c_str()); @@ -176,6 +176,10 @@ void mqttConnect() { DEBUG_MSG("\n"); mqtt.connect(); + free(host); + free(user); + free(pass); + } }