alanxz / rabbitmq-c

RabbitMQ C client
MIT License
1.76k stars 669 forks source link

Memory leak in listen example #381

Open alanxz opened 7 years ago

alanxz commented 7 years ago

From the mailing list: https://groups.google.com/forum/#!topic/rabbitmq-c-users/x6ivLTl0rt4

I would just like to inform you about a (very little) memory leak in the Listen sample (and maybe in others).

It misses a amqp_bytes_free(queuename) to release the memory allocated by queuename = amqp_bytes_malloc_dup(r->queue);

dataf3l commented 5 years ago

Just so everyone knows, this doesn't mean there is a leak in the listener within the loop, and that the program will crash, it just means that the example listener program leaks the queue name (just a few bytes), this does not cause an error in normal operation:

screen shot 2019-02-09 at 3 20 23 pm

however, if someone wants to not even have that, I tried to fix it this way:

  1. change the listenq example, line that looks like this: amqp_basic_consume(conn, 1, amqp_cstring_bytes(queuename), amqp_empty_bytes, 0, 0, 0, amqp_empty_table);

becomes this: amqp_bytes_t qn = amqp_cstring_bytes(queuename); amqp_basic_consume(conn, 1, qn, amqp_empty_bytes, 0, 0, 0, amqp_empty_table);

and at the end of main, before the

return 0,

add this: amqp_bytes_free(qn); return 0;

Anybody that knows more C++ than I do, feel free to correct my mistakes.

after seeing how the memory usage increased on XCode, I assumed this meant this was a problem, turns out I was wrong.

I hope this saves people time.