Skip to content

Commit

Permalink
Mqtt: Fixed memory leak when pushing messages to the queue when it is…
Browse files Browse the repository at this point in the history
… full. (#1612)
  • Loading branch information
etmmahi authored and slaff committed Feb 11, 2019
1 parent e4a8365 commit 81f5d10
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Sming/SmingCore/Network/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ bool MqttClient::connect(const URL& url, const String& clientName, uint32_t sslO

bool MqttClient::publish(const String& topic, const String& content, uint8_t flags)
{
if(requestQueue.full()) {
return false;
}

mqtt_message_t* message = (mqtt_message_t*)malloc(sizeof(mqtt_message_t));
mqtt_message_init(message);
message->common.type = MQTT_TYPE_PUBLISH;
Expand All @@ -255,6 +259,10 @@ bool MqttClient::publish(const String& topic, IDataSourceStream* stream, uint8_t
return false;
}

if(requestQueue.full()) {
return false;
}

mqtt_message_t* message = (mqtt_message_t*)malloc(sizeof(mqtt_message_t));
mqtt_message_init(message);
message->common.type = MQTT_TYPE_PUBLISH;
Expand All @@ -274,6 +282,10 @@ bool MqttClient::subscribe(const String& topic)
{
debug_d("subscription '%s' registered", topic.c_str());

if(requestQueue.full()) {
return false;
}

mqtt_message_t* message = (mqtt_message_t*)malloc(sizeof(mqtt_message_t));
mqtt_message_init(message);
message->common.type = MQTT_TYPE_SUBSCRIBE;
Expand All @@ -289,6 +301,10 @@ bool MqttClient::unsubscribe(const String& topic)
{
debug_d("unsubscribing from '%s'", topic.c_str());

if(requestQueue.full()) {
return false;
}

mqtt_message_t* message = (mqtt_message_t*)malloc(sizeof(mqtt_message_t));
mqtt_message_init(message);
message->common.type = MQTT_TYPE_SUBSCRIBE;
Expand Down
7 changes: 6 additions & 1 deletion Sming/Wiring/FIFO.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class FIFO : public Countable<T>
return numberOfElements;
}

bool full() const
{
return (count() >= rawSize);
}

const T &operator[](unsigned int index) const
{
return raw[index]; /* unsafe */
Expand All @@ -67,7 +72,7 @@ FIFO<T, rawSize>::FIFO() : size(rawSize)
template<typename T, int rawSize>
bool FIFO<T, rawSize>::enqueue(T element)
{
if (count() >= rawSize)
if (full())
{
return false;
}
Expand Down

0 comments on commit 81f5d10

Please sign in to comment.