From b8022f58a431d74677b78155c2c4a75dd346127d Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 2 Jan 2023 00:10:40 +0100 Subject: [PATCH] tcp_client is now a unique_ptr --- src/TinyMqtt.cpp | 12 +++++------- src/TinyMqtt.h | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 47c254b..ed44ebc 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -38,12 +38,12 @@ MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client) connect(local_broker); debug("MqttClient private with broker"); #ifdef TINY_MQTT_ASYNC - tcp_client = new_client; + tcp_client.reset(new_client); tcp_client->onData(onData, this); // client->onConnect() TODO // client->onDisconnect() TODO #else - tcp_client = new WiFiClient(*new_client); + tcp_client.reset(new WiFiClient(*new_client)); #endif alive = millis()+5000; } @@ -59,7 +59,6 @@ MqttClient::MqttClient(MqttBroker* local_broker, const std::string& id) MqttClient::~MqttClient() { close(); - delete tcp_client; debug("*** MqttClient delete()"); } @@ -99,8 +98,7 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka) debug("MqttClient::connect_to_host " << broker << ':' << port); keep_alive = ka; close(); - if (tcp_client) delete tcp_client; - tcp_client = new TcpClient; + tcp_client.reset(new TcpClient); #ifdef TINY_MQTT_ASYNC tcp_client->onData(onData, this); @@ -110,7 +108,7 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka) if (tcp_client->connect(broker.c_str(), port)) { debug("link established"); - onConnect(this, tcp_client); + onConnect(this, tcp_client.get()); } else { @@ -580,7 +578,7 @@ void MqttClient::processMessage(MqttMessage* mesg) case MqttMessage::Type::Publish: #if TINY_MQTT_DEBUG - Console << "publish " << (mqtt_flags & FlagConnected) << '/' << (long) tcp_client << endl; + Console << "publish " << (mqtt_flags & FlagConnected) << '/' << (long) tcp_client.get() << endl; #endif if ((mqtt_flags & FlagConnected) or tcp_client == nullptr) { diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 0d0448b..9d160de 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -303,7 +303,7 @@ class MqttClient // when MqttBroker uses MqttClient for each external connexion MqttBroker* local_broker=nullptr; - TcpClient* tcp_client=nullptr; // connection to remote broker + std::unique_ptr tcp_client; // connection to remote broker std::set subscriptions; std::string clientId; CallBack callback = nullptr;