eclipse/paho.mqtt.c

Memory leak when broker gets disconnected

jchabod opened this issue · 5 comments

Describe the bug
When the broker goes off, the "listening" connection suffers memory leaks.

To Reproduce
See source code example attached:

  • build (with ASAN or any memory tool)
  • start a broker (I used MOSQUITTO)
  • start the application
  • use another application to produce messages on the expected subject and check that the demo application receives these messages.
  • stop mosquitto (Application gets disconnected)
  • restart MOSQUITTO
  • stop the application (single CTRL+C)

Log files
See attachments (Just an extract here)

Indirect leak of 48 byte(s) in 1 object(s) allocated from:
    #0 0x49225f in malloc (/home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/build/bin/jch_demo+0x49225f)
    #1 0x4f50e1 in TreeAddByIndex (/home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/build/bin/jch_demo+0x4f50e1)
    #2 0x4f5202 in TreeAdd (/home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/build/bin/jch_demo+0x4f5202)
    #3 0x4f40d9 in mymalloc (/home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/build/bin/jch_demo+0x4f40d9)
    #4 0x4ef236 in ListInitialize (/home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/build/bin/jch_demo+0x4ef236)
    #5 0x4e6b62 in Socket_outInitialize (/home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/build/bin/jch_demo+0x4e6b62)
    #6 0x4d5c19 in MQTTAsync_createWithOptions (/home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/build/bin/jch_demo+0x4d5c19)
    #7 0x4d601b in MQTTAsync_create (/home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/build/bin/jch_demo+0x4d601b)
    #8 0x4d4686 in startup /home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/samples/ClientServer/demo_client/jch_demo.c:124
    #9 0x4d562f in main /home/chabod/PROJECTS/C838_S2OPC/GIT/S2OPC/samples/ClientServer/demo_client/jch_demo.c:214
    #10 0x7fedf5218082 in __libc_start_main ../csu/libc-start.c:308

Environment

  • Linux
  • PAHO v1.3.4

Additional context
I precise that there is no issue when the broker does not stop or if no messages are received

jumoog commented

v1.3.4 is very old try with a newer version

One thing you're not doing which you need to do is to free the memory for incoming messages with calls to:

MQTTAsync_freeMessage(&message);
MQTTAsync_free(topicName);

as shown in the paho_c_sub.c and other samples.

You seem to be right @icraggs ... I just do not understand why the issue did not appear before, or when the broker does not disconnect.

Maybe the leak was just not detected by ASAN...

I'll recheck that the test does not leak with that modification, and I'll close the ticket if it's OK !

Thanks

Solved, thanks!