refactor: use unique_ptr for the MQTT client

This commit is contained in:
Yurii committed 2026-11-25 22:24:21 +03:00
1 parent 72a8cd0eb1
commit aeac5bf726
1 file changed
+3 -4
+3 -4
View File
@@ -20,7 +20,7 @@
std::mutex mqttMutex; std::mutex mqttMutex;
static MQTT* mqttClient = nullptr; static std::unique_ptr<MQTT> mqttClient;
// Keep the applied prefix separate from settings until MQTT is reconfigured. // Keep the applied prefix separate from settings until MQTT is reconfigured.
static std::string mqttPrefix; // NOLINT(runtime/string) static std::string mqttPrefix; // NOLINT(runtime/string)
@@ -534,8 +534,7 @@ void stopMqtt(bool destroy = false) {
} }
haDiscovery.reset(); haDiscovery.reset();
delete mqttClient; mqttClient.reset();
mqttClient = nullptr;
std::string().swap(mqttPrefix); std::string().swap(mqttPrefix);
} }
@@ -682,7 +681,7 @@ static void connect() {
if (!mqttClient) { if (!mqttClient) {
std::lock_guard<std::mutex> lock(mqttMutex); std::lock_guard<std::mutex> lock(mqttMutex);
mqttClient = new MQTT(); mqttClient = std::make_unique<MQTT>();
mqttClient->setAsync(true); mqttClient->setAsync(true);
mqttClient->setConfigHook([](esp_mqtt_client_config_t& config) { mqttClient->setConfigHook([](esp_mqtt_client_config_t& config) {
config.session.last_will.qos = 1; config.session.last_will.qos = 1;