eclipse/paho.mqtt.c

Memory leak when using MQTTCLIENT_PERSISTENCE_USER

cuttlfish opened this issue · 3 comments

Is your feature request related to a problem? Please describe.
When using MQTTCLIENT_PERSISTENCE_USER mode, the app is now responsible for caching persistent data, which also means during startup, persist.pget is done by the app and any memory return to paho is allocated by the app. This causes a memory leak because MQTTPersistent ends up with the allocated pointer and has to free it. This freeing is then done by heap.c which attempts to track memory allocation. In this case, the memory wasn't allocated by paho obviously so Heap.c doesn't free and there is a memory leak

Describe the solution you'd like
I think there are two solutions

  1. Paho can expose a allocation function for apps to use in this case
  2. Should be a way in MQTTPersistent struct to indicate that the app will be allocating the memory so free it regardless

Describe alternatives you've considered
Open to other solutions

Can't you use:

LIBMQTT_API void* MQTTAsync_malloc(size_t size);

I am using the MQTT synchronous library, and it seems that only has

LIBMQTT_API void MQTTClient_free(void* ptr);

And not the matching MQTTClient_malloc call

Ok yes the MQTTClient API needs a malloc