This section provides information on memory management.
When sending messages, memory is allocated by the application, passed to the publish function call (in the topic and payload parameters), and then can be reused or disposed of immediately. In the case of publications at QoS 1 and 2, a copy is taken by the client of the whole message so that the retries necessary for reliability can be attempted. These copies are freed after the publish exchange for that message has been completed (as indicated by waitForCompletion
The memory allocated for topic, payload, and message structure when passed to the application in the messageArrived
callback or receive function is the responsibility of the application to manage. That is, the application must free the message structure, topic name, and payload memory blocks itself. A utility function freeMessage
is provided to simplify this by freeing the message structure and payload within it in one call.
Parent topic: Using the MQTT C Client