From 469306ff1f96bc2fa36e7b3f2a28651ca097cb14 Mon Sep 17 00:00:00 2001 From: Andy Taylor Date: Sun, 8 Mar 2026 14:16:34 +0000 Subject: [PATCH] Add mosquitto_loop_stop before mosquitto_destroy in close() The background network thread started by mosquitto_loop_start() was not being stopped before mosquitto_destroy(), which can cause a use-after-free if the thread is still running when the mosquitto structure is freed. --- MQTTConnection.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/MQTTConnection.cpp b/MQTTConnection.cpp index df009ff..a479a69 100644 --- a/MQTTConnection.cpp +++ b/MQTTConnection.cpp @@ -147,6 +147,7 @@ void CMQTTConnection::close() { if (m_mosq != nullptr) { ::mosquitto_disconnect(m_mosq); + ::mosquitto_loop_stop(m_mosq, true); ::mosquitto_destroy(m_mosq); m_mosq = nullptr; }