unmanaged c++ code - tool to detect memory leaks RRS feed

  • Question

  • Hi,

    we have unmanaged c++ code which is socket server (IOCP communication used).

    client sends data(request) to the server and server process the requests and forward to other threads.(a maximum of 12 to 15 thread for the socket server process)

    The problem is the application get crashed after a period of time.The pattern for crash keeps varryies for each time. While checking the crash dump in visual studio, the line shows at HeapAlloc statement.

    For each request from the client, a new object will be created and after sending the response the object will be deleted. From the crash dump analysis, we suspect something like heap corruption.

    The objects and its pointers get initialized to null value before and after use.

    1.How to detect these type of error in the application?

    2.There are various tools available in the web, any suggesstion on which one to use for this.

    3.The socket server application runs as windows service and we are trying to run this in IDE in debug mode. the tools like ApplicationVerifiers expects the app to run in debug mode in ide.

    4.Also the socket server application refers methods from dll files.


    Thursday, February 5, 2015 7:15 AM


All replies

  • Hi,

    A good way is to use Valgrind + Wine to debug windows code and find leaks:


    Other tools are:

    1) Purify (commercial) - http://www-01.ibm.com/software/rational/products/purifyplus_divestiture/

    2) Insure++ (commercial) - http://www.parasoft.com/product/insure/

    3) Dr. Memory (OSS) - http://code.google.com/p/drmemory/

    For other tools, please check the below link:


    Hope that helps.

    (Please mark as answer if it resolves your query. Please upvote if this post is helpful.)


    • Marked as answer by Shu 2017 Monday, February 16, 2015 11:22 AM
    Thursday, February 5, 2015 7:24 AM
  • Visual Leak Detector which is available for no cost, will detect most of the memory leaks, in cases like we allocate memory using new/malloc and forget to delete/free the same.

    Not sure how much it will be helpful for your case.

    Please see the details about the library as mentioned in above mentioned site as below:


    • Provides a complete stack trace for each leaked block, including source file and line number information when available.
    • Provides complete data dumps (in hex and ASCII) of leaked blocks.
    • Customizable level of detail in the memory leak report.
    • Visual Leak Detector is cleanly packaged as an easy-to-use library. You don't need to compile its source code to use it. And you only need to make minor additions to your own source code to integrate it with your program.
    • In addition to providing stack traces with source files, line numbers, and function names, Visual Leak Detector also provides data dumps.
    • It works with both C++ and C programs (compatible with both new/delete and malloc/free).
    • The full source code to the library is included and it is well documented, so it is easy to customize it to suit your needs.


    Thanks & Regards

    Thursday, February 5, 2015 11:41 AM
  • Hi

    The easiest way to detect such errors is using your own kind of memory management for the type of objects that you suspect. So, instead of using malloc/free, you can use my_alloc/my_free. Then add some marker areas around them (e.g. instead of allocating 100 byte allocate 200 and return the 100 in the middle of the block) and the free function could keep the pointer (not deleting the block), writing a pattern into it and check all the markers of all old blocks... like this it could be possible to find the bug. Or use VirtualAlloc and place guard-pages around your objects, so that you can immediately see when wrong accesses occur...

    Just some ideas... maybe one helps you to find a way that works for you...

    Thursday, February 5, 2015 3:57 PM