Fixed memory leak

This commit is contained in:
Gunnar Skjold
2025-01-23 11:22:03 +01:00
parent f5178459e8
commit f7596de166

View File

@@ -117,20 +117,20 @@ bool AmsMqttHandler::connect() {
if ((strlen(mqttConfig.username) == 0 && mqtt.connect(mqttConfig.clientId)) || if ((strlen(mqttConfig.username) == 0 && mqtt.connect(mqttConfig.clientId)) ||
(strlen(mqttConfig.username) > 0 && mqtt.connect(mqttConfig.clientId, mqttConfig.username, mqttConfig.password))) { (strlen(mqttConfig.username) > 0 && mqtt.connect(mqttConfig.clientId, mqttConfig.username, mqttConfig.password))) {
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO)) if (debugger->isActive(RemoteDebug::INFO))
#endif #endif
debugger->printf_P(PSTR("Successfully connected to MQTT\n")); debugger->printf_P(PSTR("Successfully connected to MQTT\n"));
mqtt.onMessage(std::bind(&AmsMqttHandler::onMessage, this, std::placeholders::_1, std::placeholders::_2)); mqtt.onMessage(std::bind(&AmsMqttHandler::onMessage, this, std::placeholders::_1, std::placeholders::_2));
if(strlen(mqttConfig.subscribeTopic) > 0) { if(strlen(mqttConfig.subscribeTopic) > 0) {
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO)) if (debugger->isActive(RemoteDebug::INFO))
#endif #endif
debugger->printf_P(PSTR(" Subscribing to [%s]\n"), mqttConfig.subscribeTopic); debugger->printf_P(PSTR(" Subscribing to [%s]\n"), mqttConfig.subscribeTopic);
if(!mqtt.subscribe(mqttConfig.subscribeTopic)) { if(!mqtt.subscribe(mqttConfig.subscribeTopic)) {
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR)) if (debugger->isActive(RemoteDebug::ERROR))
#endif #endif
debugger->printf_P(PSTR(" Unable to subscribe to to [%s]\n"), mqttConfig.subscribeTopic); debugger->printf_P(PSTR(" Unable to subscribe to to [%s]\n"), mqttConfig.subscribeTopic);
} }
} }
mqtt.publish(statusTopic, "online", true, 0); mqtt.publish(statusTopic, "online", true, 0);
@@ -138,9 +138,9 @@ debugger->printf_P(PSTR(" Unable to subscribe to to [%s]\n"), mqttConfig.subscr
return true; return true;
} else { } else {
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR)) if (debugger->isActive(RemoteDebug::ERROR))
#endif #endif
{ {
debugger->printf_P(PSTR("Failed to connect to MQTT: %d\n"), mqtt.lastError()); debugger->printf_P(PSTR("Failed to connect to MQTT: %d\n"), mqtt.lastError());
#if defined(ESP8266) #if defined(ESP8266)
if(mqttSecureClient) { if(mqttSecureClient) {
@@ -156,6 +156,10 @@ if (debugger->isActive(RemoteDebug::ERROR))
void AmsMqttHandler::disconnect() { void AmsMqttHandler::disconnect() {
mqtt.disconnect(); mqtt.disconnect();
mqtt.loop(); mqtt.loop();
if(mqttSecureClient != NULL) {
delete mqttSecureClient;
mqttSecureClient = NULL;
}
delay(10); delay(10);
yield(); yield();
} }