Esp_mqtt_client: when can I remove the sent data from the heap

Good afternoon!

When using ESP-IDF, there is a library called esp_mqtt_client. It has a function

int esp_mqtt_client_enqueue (esp_mqtt_client_handle_tclient, const char * topic, const char * data, int len, int qos, int retain, bool store)

which allows you to queue a message for subsequent sending to the broker.

QUESTION: At what point can char * topic and const char * data be removed from the heap if they were dynamically created at the time of sending (using malloc for example) ???

PS: Sorry for the English, I use a translator.

Согласно документам на Page not Found - ESP32 - — ESP-IDF Programming Guide latest documentation

esp_mqtt_client_enqueue

Поместить сообщение в папку «Исходящие», чтобы отправить его позже. Обычно используется для сообщений с qos> 0, но может также использоваться для сообщений qos = 0, если store = true.

Этот API генерирует и сохраняет сообщение публикации во внутреннем ящике исходящих сообщений, а фактическая отправка в сеть выполняется в контексте задачи mqtt (в отличие от esp_mqtt_client_publish (), которая отправляет сообщение публикации немедленно в контексте задачи пользователя). Таким образом, его можно использовать как неблокирующую версию esp_mqtt_client_publish ().

Возвращение

Message_id, если успешно поставлен в очередь, иначе -1

Сообщение помещено в очередь исходящих. Я предполагаю, что вы можете удалить данные из кучи, если сообщение успешно поставлено в очередь.

Сказав это, я обязательно проведу несколько тестов. А также проверьте документацию на наличие функции, которая сообщает мне статус сообщения в очереди - если оно есть.

HTH

Ваше здоровье,
Норма.

According to the docs at Page not Found - ESP32 - — ESP-IDF Programming Guide latest documentation

esp_mqtt_client_enqueue

Enqueue a message to the outbox, to be sent later. Typically used for messages with qos>0, but could be also used for qos=0 messages if store=true.

This API generates and stores the publish message into the internal outbox and the actual sending to the network is performed in the mqtt-task context (in contrast to the esp_mqtt_client_publish() which sends the publish message immediately in the user task’s context). Thus, it could be used as a non blocking version of esp_mqtt_client_publish().

Return

Message_id if queued successfully, -1 otherwise

The message appears to be queued to the outbox. I assume then that you can remove the data from the heap if the message successfully queued.

Having said that, I would definitely run a few tests. And also, check the docs for a function that tells me the status if a queued message – if there is one.

HTH

Cheers,
Norm.

Thank you very much. I also came to the same conclusion when I traced the path through the mqtt_client and mqtt_msg sources.

1 Like