ZeroMQ Server中的内存泄漏[Request-Reply Scenario]

时间:2016-10-21 09:35:18

标签: visual-c++ memory-leaks zeromq

我正在测试ØMQ以便在我的项目中使用。我从here下载了最新的二进制文件。我使用libzmq-v90-mt-gd-4_0_4.lib作为预期的项目用途C++ tool-set v90。现在我创建了一个示例服务器,例如official zero-mq guide中给出的示例和同一guide中给出的客户端代码。服务器代码是:

void zmqServer()
{
    //  Prepare our context and socket
    zmq::context_t context(1);
    zmq::socket_t socket(context, ZMQ_REP);
    socket.bind("tcp://*:5555");
    //int i = 0;
    while (true) {
        zmq::message_t request;

        char buffer[256];
        //  Wait for next request from client
        int size = zmq_recv(socket, buffer, 255, 0);
        if (size == -1) continue;
        if (size > 255) size = 255;
        buffer[size] = 0;
        std::string receivedStr = strdup(buffer);

        std::cout << "Message # " << i << ": " << receivedStr << std::endl;

        //  Do some 'work'
        sleep(1);

        //  Send reply back to client
        zmq::message_t reply(5);
        std::string msg = "Server#" + receivedStr;
        std::cout << "Reply # " << i << ": " << msg << std::endl;
        memcpy(reply.data(), msg.c_str(), msg.length());
        socket.send(reply);
        //i++;
    }
}

客户端代码是:

    void zmqClient()
    {
        //  Prepare our context and socket
        zmq::context_t context(1);
        zmq::socket_t socket(context, ZMQ_REQ);

        std::cout << "Connecting to hello world server..." << std::endl;
        socket.connect("tcp://localhost:5555");

        //  Do 10 requests, waiting each time for a response
        for (int request_nbr = 0; request_nbr != 10; request_nbr/*++ removed to loop forever to test*/) {
            std::string msg = "HELLO FROM CLIENT";
            zmq::message_t request(msg.length());
            memcpy(request.data(), msg.c_str(), msg.length());
            std::cout << "Sending Request#: " << request_nbr << "\nMessage: " << msg << std::endl;
            socket.send(request);

            //  Get the reply.
            zmq::message_t reply;
            socket.recv(&reply);
            std::cout << "Received World " << request_nbr << std::endl;
        }
    }

在服务器端,Windows诊断工具(vs-2015)快照一次是:

windows diagnostic tool at 15:11

稍后的另一个快照是:

windows diagnostic tool at 22:06

可以看到两次之间的记忆差异。关于另一次运行,在20个服务器程序连接之后,Visual Leak Detector报告是:

WARNING: Visual Leak Detector detected memory leaks!
---------- Block 7 at 0x0171DFE8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 10 at 0x0171E0E8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 17 at 0x0171E128: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 19 at 0x0171E1A8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 13 at 0x0171E228: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 14 at 0x0171E2E8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 1 at 0x0171E368: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 6 at 0x0171E3A8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 15 at 0x0171E3E8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 16 at 0x0171E428: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 20 at 0x0171E468: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 4 at 0x0171E4E8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 11 at 0x0171E528: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 2 at 0x0171E5A8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 5 at 0x0171E5E8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 12 at 0x0171E628: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 18 at 0x0171E668: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 3 at 0x0171E6E8: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 8 at 0x0171E728: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


---------- Block 9 at 0x0171E768: 18 bytes ----------
  Leak Hash: 0x90285749, Count: 1, Total 18 bytes
  Call Stack (TID 5340):
    MSVCR90D.dll!strdup()
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (46): ZeroMQServer.exe!zmqServer() + 0xF bytes
    c:\users\awais\documents\visual studio 2015\projects\zeromqserver\zeromqserver\zmq_server.cpp (20): ZeroMQServer.exe!main()
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): ZeroMQServer.exe!__tmainCRTStartup() + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): ZeroMQServer.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
    ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
  Data:
    48 45 4C 4C    4F 20 46 52    4F 4D 20 43    4C 49 45 4E     HELLO.FR OM.CLIEN
    54 00                                                        T....... ........


Visual Leak Detector detected 20 memory leaks (1080 bytes).
Largest number used: 1080 bytes.
Total allocations: 1080 bytes.
Visual Leak Detector is now exiting.

可以看到每个连接都有内存泄漏。在这种情况下,任何建议谁是罪魁祸首?是memcpy()有点像其他人,我发现有关zero-mq memory leak的讨论。任何克服这种情况的建议都将受到高度赞赏。

修改

memcpy()也在客户端使用,但 Visual Leak Detector 未报告内存泄漏。

1 个答案:

答案 0 :(得分:1)

找到了罪魁祸首。这一行:

std::string receivedStr = strdup(buffer);

strdup()分配内存并返回reference作为this问题状态和答案的答案。另一个来自man page。我应该这样做:

char* receivedStr = strdup(buffer);
// use receivedStr
free(receivedStr);

我今天第一次使用strdup()。虽然这是一个新手的琐碎错误。