Skip to content

Commit

Permalink
Fix memory leak for broker to remote broker connection
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaturn committed Apr 10, 2023
1 parent 3c77f7c commit d3bf379
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/TinyMqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ MqttBroker::~MqttBroker()
#ifdef EPOXY_DUINO
instances--;
#endif
closeRemoteBroker();
while(clients.size())
{
auto client = clients[0];
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/TinyMqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ class MqttClient
~MqttClient();

void connect(MqttBroker* local_broker);
void connect(string broker, uint16_t port, uint16_t keep_alive = 10);
void connect(const IPAddress& ip, 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 = 1883, uint16_t keep_alive = 10)
{ connect(ip.toString().c_str(), port, keep_alive); }

// TODO it seems that connected returns true in tcp mode even if
Expand Down Expand Up @@ -387,6 +387,8 @@ class MqttBroker
const char* auth_password = "guest";
MqttClient* remote_broker = nullptr;

void closeRemoteBroker();

void retain(const Topic& topic, const MqttMessage& msg);
void retainDrop();

Expand Down

0 comments on commit d3bf379

Please sign in to comment.