How to use Application Verifier to find memory leaks

21,157

Solution 1

CRT memory leaks detection (without stack trace):

// debug_new.h
#pragma once

#include "crtdbg.h"

#ifdef _DEBUG
#ifndef DEBUG_NEW
#define DEBUG_NEW   new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#endif

All .cpp files:

#include "debug_new.h"

...

// After all other include lines:
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

...

Write this once in the program initialization code:

_CrtSetDbgFlag( _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);

In MFC, all this is already implemented in MFC headers. You only need to ensure, that every cpp file contains these lines:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

Restrictions: this catches only "new" memory leaks, all leaks, caused by another functions, like malloc, are not caught.

Don't make any allocations inside of .h files - they will be printed without source lines, because DEBUG_NEW is defined after all #include lines.

Solution 2

Application Verifier only catches leaks in DLLs. Try to read the tooltip in the leak checkbox. That's what it says.

Solution 3

Memory Validator from Software Verification will catch memory leaks, and show the complete callstack from the leak's allocation. While it is a commercial product, it has a trial period so programmers can try it and see if it is worth the price to them.

Solution 4

I have a feeling that Application Verifier special cases the exit path and doesn't flag these as leaks - after all, the entire process heap is free on process exit.

Try writing another sample where you initialize the same pointer again - basically lose the reference to the previous allocation. That should certainly be flagged. Let me know the results.

Also, AppVerifier (if you have all the options enabled) should also catch buffer overflows, underflows, writing to stack locations marked RO etc.

Share:
21,157

Related videos on Youtube

Patrick
Author by

Patrick

Die-hard developer. Not afraid to go low-level (even down to assembly when needed during debugging). Prefers readable code over short, complex, 'would-be-smart', unreadable code. Musical preferences: EBM (Front 242), Synthpop (Human League, Heaven 17), Electronic stuff (Kraftwerk) and most things that don't fit mainstream (Severed Heads, Isao Tomita). Likes 80's pinballs (Comet, High Speed)

Updated on January 14, 2022

Comments

  • Patrick
    Patrick over 2 years

    I want to find memory leaks in my application using standard utilities. Previously I used my own memory allocator, but other people (yes, you AlienFluid) suggested to use Microsoft's Application Verifier, but I can't seem to get it to report my leaks. I have the following simple application:

    #include <iostream>
    #include <conio.h>
    
    class X
       {
       public:
          X::X() : m_value(123) {}
       private:
          int m_value;
       };
    
    void main()
    {
    X *p1 = 0;
    X *p2 = 0;
    X *p3 = 0;
    
    p1 = new X();
    p2 = new X();
    p3 = new X();
    delete p1;
    delete p3;
    }
    

    This test clearly contains a memory leak: p2 is new'd but not deleted.

    I build the executable using the following command lines:

    cl /c /EHsc /Zi /Od /MDd test.cpp
    link /debug test.obj
    

    I downloaded Application Verifier (4.0.0665) and enabled all checks.

    If I now run my test application I can see a log of it in Application Verifier, but I don't see the memory leak.

    Questions:

    • Why doesn't Application Verifier report a leak?
    • Or isn't Application Verifier really intended to find leaks?
    • If it isn't which other tools are available to clearly report leaks at the end of the application (i.e. not by taking regular snapshots and comparing them since this is not possible in an application taking 1GB or more), including the call stack of the place of allocation (so not the simple leak reporting at the end of the CRT)

    If I don't find a decent utility, I still have to rely on my own memory manager (which does it perfectly).

    • Alex F
      Alex F
      I also think that memory leaks detection is not a reason for writing your own memory allocator. CRT gives everything except stack trace - if you are interesting, I can post you the code.
    • Patrick
      Patrick
      Problem is that I have a perfectly working, self-written, memory allocator that's quite fast, logs all memory allocations including call stacks, report leaks (including call stack) at the end of the application, checks for buffer overflows/underflows, ..., BUT everyone (on StackOverflow) seems to indicate that you MUSTN'T write your own memory manager as the standard one of the CRT/Windows is good enough, and there are enough utilities to find memory leaks, overwrites, ... However, I can't seem to get them working.
  • Alessandro Ricci
    Alessandro Ricci almost 14 years
    BTW, also try the "Global Flags" utility that comes with the Debugging Tools for Windows in the Windows SDK. It has a bunch of options for heap tagging etc. and it might be what you need.
  • Patrick
    Patrick almost 14 years
    I tried setting the pointer to 0. Nothing reported in Application Verifier. Tried using GFLAGS to enable all memory-related flags. Nothing reported. It seems that it is simply impossible to get memory leaks at exit (except for the very minimal leak-reporting in the CRT). Seems I have to stick to my own memory manager.
  • Patrick
    Patrick almost 14 years
    This trick doesn't work for malloc/free. One of the include files of the CRT does something similar with MALLOC and FREE (#define free DEBUG_FREE or something like this), and this gives problems with classes that have a method called free (e.g. in Qt). Also, this also writes out the memory leaks, but without call stacks, and I really need the call stacks.
  • Sergey Skopus
    Sergey Skopus over 7 years
    it's rather difficult to work with VLD when C++ singletones are used

Related