zeromq / azmq

C++ language binding library integrating ZeroMQ with Boost Asio
Boost Software License 1.0
318 stars 108 forks source link

azmq::message's move assignment operator leaks memory. #144

Open JenSte opened 6 years ago

JenSte commented 6 years ago

The move assignment operator of azmq::message is broken:

The following program demonstrates the problems:

#include <azmq/message.hpp>
#include <iostream>

int main()
{
    int major, minor, patch;
    zmq_version(&major, &minor, &patch);

    std::cout << "boost version: " << BOOST_LIB_VERSION << '\n'
              << "zmq version: " << major << '.' << minor << '.' << patch << '\n'
              << "azmq version: git-a8f54cc8\n"
              << "gcc version: " << __GNUC__ << '.' << __GNUC_MINOR__ << '.' << __GNUC_PATCHLEVEL__ << std::endl;

    azmq::message m1(50);
    azmq::message m2(60);

    // Internal message of 'm2' is not freed.
    m2 = std::move(m1);

    azmq::message m3(70);

    std::cout << "m3.data() = " << m3.data() << '\n'
              << "m3.size() = " << m3.size() << std::endl;

    // Self-assignment, original (70 bytes long) buffer lost.
    m3 = std::move(m3);

    std::cout << "m3.data() = " << m3.data() << '\n'
              << "m3.size() = " << m3.size() << std::endl;
}

Output:

boost version: 1_63
zmq version: 4.1.6
azmq version: git-a8f54cc8
gcc version: 7.3.1
m3.data() = 0x60b000000118
m3.size() = 70
m3.data() = 0x7ffcd071dad0
m3.size() = 0

=================================================================
==3499==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 210 byte(s) in 2 object(s) allocated from:
    #0 0x7f15e0a66850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7f15e022d1bf  (/lib64/libzmq.so.5+0x261bf)

SUMMARY: AddressSanitizer: 210 byte(s) leaked in 2 allocation(s).