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
instances--;
#endif
closeRemoteBroker();
while(clients.size())
{
auto client = clients[0];
@@ -155,9 +156,19 @@ void MqttBroker::addClient(MqttClient* 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)
{
debug("MqttBroker::connect");
closeRemoteBroker();
if (remote_broker == nullptr) remote_broker = new MqttClient;
remote_broker->connect(host, port);
remote_broker->local_broker = this; // Because connect removed the link