From 0975e6bd0b1ca4b88b095de464727c3f825fd488 Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Wed, 30 Aug 2023 21:09:38 +0200 Subject: [PATCH] cleanup outbox on allocation fail --- src/MqttClient.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/MqttClient.h b/src/MqttClient.h index 8d89aba..6613962 100644 --- a/src/MqttClient.h +++ b/src/MqttClient.h @@ -148,16 +148,24 @@ class MqttClient { bool _addPacket(Args&&... args) { espMqttClientTypes::Error error(espMqttClientTypes::Error::SUCCESS); espMqttClientInternals::Outbox::Iterator it = _outbox.emplace(0, error, std::forward(args) ...); - if (it && error == espMqttClientTypes::Error::SUCCESS) return true; - return false; + if (it && error == espMqttClientTypes::Error::SUCCESS) { + return true; + } else { + if (it) _outbox.remove(it); + return false; + } } template bool _addPacketFront(Args&&... args) { espMqttClientTypes::Error error(espMqttClientTypes::Error::SUCCESS); espMqttClientInternals::Outbox::Iterator it = _outbox.emplaceFront(0, error, std::forward(args) ...); - if (it && error == espMqttClientTypes::Error::SUCCESS) return true; - return false; + if (it && error == espMqttClientTypes::Error::SUCCESS) { + return true; + } else { + if (it) _outbox.remove(it); + return false; + } } void _checkOutbox();