amqpcpp
amqpcpp copied to clipboard
Memory leak in AMQPExchange::Publish
There is a memory leak in method Publish(). Memory allocated by amqp_bytes_malloc function is never freed. void AMQPExchange::Publish(const char * data, uint32_t length, string key) { amqp_bytes_t messageByte = amqp_bytes_malloc(length); memcpy(messageByte.bytes,data,length); sendPublishCommand(messageByte, key.c_str()); }
Three possible solutions:
- Add dealocation to the and of Publish method
- Use memory on stack
- Do not allocate memory at all. This is the best way from my point. If someone want make copy of memory, he can wrap this method.
New implementation without allocation. There is one change data is not marked as const now because "amqp_basic_publish" function body parameter is not marked const.
void AMQPExchange::Publish(char * data, uint32_t length, string key) { amqp_bytes_t messageByte; messageByte.bytes = data; messageByte.len = length; sendPublishCommand(messageByte, key.c_str()); }