mirror of
https://github.com/alexhopeoconnor/arduino-home-assistant.git
synced 2026-10-04 02:48:13 +10:00
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:
+4
-1
@@ -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
@@ -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
|
||||
|
||||
|
||||
@@ -20,6 +20,11 @@ HABaseDeviceType::HABaseDeviceType(
|
||||
}
|
||||
}
|
||||
|
||||
HABaseDeviceType::~HABaseDeviceType()
|
||||
{
|
||||
destroySerializer();
|
||||
}
|
||||
|
||||
void HABaseDeviceType::setAvailability(bool online)
|
||||
{
|
||||
_availability = (online ? AvailabilityOnline : AvailabilityOffline);
|
||||
|
||||
@@ -37,6 +37,8 @@ public:
|
||||
const char* uniqueId
|
||||
);
|
||||
|
||||
virtual ~HABaseDeviceType();
|
||||
|
||||
/**
|
||||
* Returns unique ID of the device type.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user