diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index ed44ebc..9d5715b 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -17,7 +17,7 @@ int TinyMqtt::debug=2; MqttBroker::MqttBroker(uint16_t port) { - server = new TcpServer(port); + server.reset(new TcpServer(port)); #ifdef TINY_MQTT_ASYNC server->onClient(onClient, this); #endif @@ -29,7 +29,6 @@ MqttBroker::~MqttBroker() { delete clients[0]; } - delete server; } // private constructor used by broker only @@ -165,6 +164,7 @@ void MqttBroker::onClient(void* broker_ptr, TcpClient* client) void MqttBroker::loop() { #ifndef TINY_MQTT_ASYNC + if (not server) return; WiFiClient client = server->available(); if (client) diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 9d160de..cea0eea 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -322,7 +322,7 @@ class MqttBroker MqttBroker(uint16_t port); ~MqttBroker(); - void begin() { server->begin(); } + void begin() { if (server) server->begin(); } void loop(); void connect(const std::string& host, uint16_t port=1883); @@ -361,7 +361,7 @@ class MqttBroker std::vector clients; private: - TcpServer* server = nullptr; + std::unique_ptr server; const char* auth_user = "guest"; const char* auth_password = "guest";