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.

Related