Fix MQTT client ownership and base device cleanup.

Store the production PubSubClient instance directly on HAMqtt and release base device serializers on destruction to avoid invalid deletes and leaked serializer allocations.
This commit is contained in:
2026-04-16 17:39:42 +10:00
parent d954543236
commit 7894f0c621
4 changed files with 16 additions and 3 deletions
+4 -1
View File
@@ -60,7 +60,8 @@ HAMqtt::HAMqtt(
HADevice& device,
uint8_t maxDevicesTypesNb
) :
_mqtt(new PubSubClient(netClient)),
_mqttStorage(netClient),
_mqtt(&_mqttStorage),
HAMQTT_INIT
{
_instance = this;
@@ -71,9 +72,11 @@ HAMqtt::~HAMqtt()
{
delete[] _devicesTypes;
#ifdef ARDUINOHA_TEST
if (_mqtt) {
delete _mqtt;
}
#endif
_instance = nullptr;
}
+5 -2
View File
@@ -14,7 +14,7 @@
#ifdef ARDUINOHA_TEST
class PubSubClientMock;
#else
class PubSubClient;
#include <PubSubClient.h>
#endif
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
@@ -417,7 +417,10 @@ private:
#ifdef ARDUINOHA_TEST
PubSubClientMock* _mqtt;
#else
/// Instance of the PubSubClient class. It's initialized in the constructor.
/// Storage for the PubSubClient instance used in production builds.
PubSubClient _mqttStorage;
/// Pointer to the active MQTT client implementation.
PubSubClient* _mqtt;
#endif
+5
View File
@@ -20,6 +20,11 @@ HABaseDeviceType::HABaseDeviceType(
}
}
HABaseDeviceType::~HABaseDeviceType()
{
destroySerializer();
}
void HABaseDeviceType::setAvailability(bool online)
{
_availability = (online ? AvailabilityOnline : AvailabilityOffline);
+2
View File
@@ -37,6 +37,8 @@ public:
const char* uniqueId
);
virtual ~HABaseDeviceType();
/**
* Returns unique ID of the device type.
*/