zeromq / malamute

The ZeroMQ Enterprise Messaging Broker
Mozilla Public License 2.0
324 stars 77 forks source link

malamute leaks memory, doesn't free messages in flight #319

Closed githubposter12 closed 5 years ago

githubposter12 commented 6 years ago

Hi, malamute appears to leak memory and does not properly free messages in flight. See the simple test case and valgrind output below. Am I doing something wrong, or is there a way to flush/purge the queues before destroying everything, to prevent this from happening?

Thanks.

#include <malamute.h>

int main (int argc, char *argv [])
{
    int rc=0;
    //  Let's start a new Malamute broker
    zactor_t *broker = zactor_new (mlm_server, NULL);

    //  Switch on verbose tracing... this gets a little overwhelming so you
    //  can comment or delete this when you're bored with it:
    zsock_send (broker, "s", "VERBOSE");

   zstr_sendx (broker, "BIND", "tcp://*:12345", NULL);

    mlm_client_t *client1 = mlm_client_new ();
    assert (client1);
    rc = mlm_client_connect (client1, "tcp://127.0.0.1:12345", 1000, "client1");
    assert (rc == 0);

    mlm_client_t *client2 = mlm_client_new ();
    assert (client2);
    rc = mlm_client_connect (client2, "tcp://127.0.0.1:12345", 1000, "client2");
    assert (rc == 0);

    zmsg_t*zmsg= zmsg_new();
    zmsg_addstr(zmsg, "hello");
    mlm_client_sendto(client1, "client2", "subject", "tracker", 0, &zmsg);
    mlm_client_destroy(&client1);
    mlm_client_destroy(&client2);
    zactor_destroy(&broker);
    return 0;
}
u12345@ihatebill:~/src/malamute/build$ gcc -g try.c -o try -lmlm -lczmq -lzmq
u12345@ihatebill:~/src/malamute/build$ valgrind --leak-check=full ./try
==9624== Memcheck, a memory error detector
==9624== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==9624== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==9624== Command: ./try
==9624== 
D: 18-11-24 10:49:58 :     API command=BIND
I: 18-11-24 10:49:58 My address is 'client1'
D: 18-11-24 10:49:58    204:                                 : start:
D: 18-11-24 10:49:58    204:                                 :     CONNECTION_OPEN
D: 18-11-24 10:49:58    204:                                 :         $ register new client
I: 18-11-24 10:49:58 client 204 address='client1' - registering
D: 18-11-24 10:49:58    204:                                 :         $ send OK
D: 18-11-24 10:49:58    204:                                 :         $ check for mailbox messages
D: 18-11-24 10:49:58    204:                                 :         > connected
I: 18-11-24 10:49:58 My address is 'client2'
D: 18-11-24 10:49:58    205:                                 : start:
D: 18-11-24 10:49:58    205:                                 :     CONNECTION_OPEN
D: 18-11-24 10:49:58    205:                                 :         $ register new client
I: 18-11-24 10:49:58 client 205 address='client2' - registering
D: 18-11-24 10:49:58    205:                                 :         $ send OK
D: 18-11-24 10:49:58    205:                                 :         $ check for mailbox messages
D: 18-11-24 10:49:58    205:                                 :         > connected
D: 18-11-24 10:49:58    204:                                 : connected:
D: 18-11-24 10:49:58    204:                                 :     MAILBOX_SEND
D: 18-11-24 10:49:58    204:                                 :         $ write message to mailbox
D: 18-11-24 10:49:58    205:                                 : connected:
D: 18-11-24 10:49:58    205:                                 :     mailbox_message
D: 18-11-24 10:49:58    205:                                 :         $ get message to deliver
D: 18-11-24 10:49:58    205:                                 :         $ send MAILBOX_DELIVER
D: 18-11-24 10:49:58    205:                                 :         $ check for mailbox messages
D: 18-11-24 10:49:58    205:                                 :         > connected
D: 18-11-24 10:49:58    204:                                 :         > connected
D: 18-11-24 10:49:58    204:                                 : connected:
D: 18-11-24 10:49:58    204:                                 :     CONNECTION_CLOSE
D: 18-11-24 10:49:58    204:                                 :         $ send OK
D: 18-11-24 10:49:58    204:                                 :         $ client closed connection
I: 18-11-24 10:49:58 client 204 address='client1' - closed connection
D: 18-11-24 10:49:58    204:                                 :         $ deregister the client
I: 18-11-24 10:49:58 client 204 address='client1' - de-registering
D: 18-11-24 10:49:58    204:                                 :         $ terminate
D: 18-11-24 10:49:58    205:                                 : connected:
D: 18-11-24 10:49:58    205:                                 :     CONNECTION_CLOSE
D: 18-11-24 10:49:58    205:                                 :         $ send OK
D: 18-11-24 10:49:58    205:                                 :         $ client closed connection
I: 18-11-24 10:49:58 client 205 address='client2' - closed connection
D: 18-11-24 10:49:58    205:                                 :         $ deregister the client
I: 18-11-24 10:49:58 client 205 address='client2' - de-registering
D: 18-11-24 10:49:58    205:                                 :         $ terminate
D: 18-11-24 10:49:58 :     API command=$TERM
==9624== 
==9624== HEAP SUMMARY:
==9624==     in use at exit: 640 bytes in 9 blocks
==9624==   total heap usage: 1,096 allocs, 1,087 frees, 746,170 bytes allocated
==9624== 
==9624== 640 (32 direct, 608 indirect) bytes in 1 blocks are definitely lost in loss record 9 of 9
==9624==    at 0x4839775: calloc (vg_replace_malloc.c:711)
==9624==    by 0x48D815F: zmsg_new (in /usr/lib/x86_64-linux-gnu/libczmq.so.4.1.1)
==9624==    by 0x48D8956: zmsg_recv (in /usr/lib/x86_64-linux-gnu/libczmq.so.4.1.1)
==9624==    by 0x48844C3: mlm_proto_recv (mlm_proto.c:1156)
==9624==    by 0x4898F81: s_client_handle_protocol (mlm_client_engine.inc:1542)
==9624==    by 0x48D79F5: zloop_start (in /usr/lib/x86_64-linux-gnu/libczmq.so.4.1.1)
==9624==    by 0x489912E: mlm_client (mlm_client_engine.inc:1580)
==9624==    by 0x48BCAB2: ??? (in /usr/lib/x86_64-linux-gnu/libczmq.so.4.1.1)
==9624==    by 0x4E39163: start_thread (pthread_create.c:486)
==9624==    by 0x4C31DEE: clone (clone.S:95)
==9624== 
==9624== LEAK SUMMARY:
==9624==    definitely lost: 32 bytes in 1 blocks
==9624==    indirectly lost: 608 bytes in 8 blocks
==9624==      possibly lost: 0 bytes in 0 blocks
==9624==    still reachable: 0 bytes in 0 blocks
==9624==         suppressed: 0 bytes in 0 blocks
==9624== 
==9624== For counts of detected and suppressed errors, rerun with: -v
==9624== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)