This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Possible memory leak when using MQTT in discontinuous fashion.

I've been experimenting with using mqtt discontinuously, and have run into an issue where it appears to leak memory, leading to a subsequent mqtt_connect failing with -12, Not enough space errors.

The sequence of events is:

mqtt_connect

subscribe/publish, etc.

mqtt_disconnect

sleep

Repeat previous steps forever. After a few dozen, or sometimes many dozen cycles of this, things go awry, apparently with memory allocation.

It appears that the memory allocated is not from the system heap, as I've been tracking that heap usage crudely by periodically probing the largest allocable block, and find that it does not change much. I've also found that once the memory leak has progressed, the at_cmd parser starts to fail as well with "Failed to send AT command (err:12)" errors.

I'm using SDK 1.2, and modem firmware version 1.2

Questions:

  1. Is there a way to hook the various memory allocators in use and monitor their state? I built a very crude monitoring system that simply allocates and frees larger and larger blocks until it fails, but there's got to be a clean way to do this. I do *NOT* observe memory leaking via this method.
  2. Am I correct in thinking that the modem offload infrastructure uses its own memory allocation, separate from the regular system malloc/free? I see that some stuff uses k_malloc and k_free, but it appears that the modem offload stuff uses something yet different.
  3. Sometimes things just stop, and when I pause the debugger it's looping endlessly in mem_alloc.constprop

What steps could I take to further debug this? Are there some recommended tools and methods? Between the Nordic and Zephyr documentation, there's quite a bit left to the imagination about how memory allocation works in this ecosystem.

Parents Reply Children
  • Hi,

    It took some time, but now we have some findings.

    Could you share a bit more about how your MQTT connection was configured? More specifically, did you use TLS, and if so, did you set the TLS hostname in the configuration?
    We've narrowed it down to the TLS hostname being the likely cause to the issues you're seeing here. It would be useful if you can try again without setting the TLS hostname and checkif that fixes your issues.

    Please be aware that that will disable hostname verification, with the possible security implications that has.

    Thanks for your patience!

    Best regards,

    Jan Tore

Related