Fix memory leak for broker to remote broker connection

This commit is contained in:
hsaturn
2023-04-10 10:43:27 +02:00
parent 3c77f7cafd
commit d3bf379d19
2 changed files with 15 additions and 2 deletions

View File

@@ -37,6 +37,7 @@ MqttBroker::~MqttBroker()
#ifdef EPOXY_DUINO #ifdef EPOXY_DUINO
instances--; instances--;
#endif #endif
closeRemoteBroker();
while(clients.size()) while(clients.size())
{ {
auto client = clients[0]; auto client = clients[0];
@@ -155,9 +156,19 @@ void MqttBroker::addClient(MqttClient* client)
clients.push_back(client); clients.push_back(client);
} }
void MqttBroker::closeRemoteBroker()
{
if (remote_broker)
{
delete remote_broker;
remote_broker = nullptr;
}
}
void MqttBroker::connect(const string& host, uint16_t port) void MqttBroker::connect(const string& host, uint16_t port)
{ {
debug("MqttBroker::connect"); debug("MqttBroker::connect");
closeRemoteBroker();
if (remote_broker == nullptr) remote_broker = new MqttClient; if (remote_broker == nullptr) remote_broker = new MqttClient;
remote_broker->connect(host, port); remote_broker->connect(host, port);
remote_broker->local_broker = this; // Because connect removed the link remote_broker->local_broker = this; // Because connect removed the link

View File

@@ -209,8 +209,8 @@ class MqttClient
~MqttClient(); ~MqttClient();
void connect(MqttBroker* local_broker); void connect(MqttBroker* local_broker);
void connect(string broker, uint16_t port, uint16_t keep_alive = 10); void connect(string broker, uint16_t port = 1883, uint16_t keep_alive = 10);
void connect(const IPAddress& ip, uint16_t port, uint16_t keep_alive = 10) void connect(const IPAddress& ip, uint16_t port = 1883, uint16_t keep_alive = 10)
{ connect(ip.toString().c_str(), port, keep_alive); } { connect(ip.toString().c_str(), port, keep_alive); }
// TODO it seems that connected returns true in tcp mode even if // TODO it seems that connected returns true in tcp mode even if
@@ -387,6 +387,8 @@ class MqttBroker
const char* auth_password = "guest"; const char* auth_password = "guest";
MqttClient* remote_broker = nullptr; MqttClient* remote_broker = nullptr;
void closeRemoteBroker();
void retain(const Topic& topic, const MqttMessage& msg); void retain(const Topic& topic, const MqttMessage& msg);
void retainDrop(); void retainDrop();