Windows program memory leakage detection tool based on CRT debug

Source: Internet
Author: User

WindowsProgramMemory leakage detection is a very important task. Gui-based applications usually have memory leakage reports at the end of debugging. However, this report is not comprehensive, the specific row number that generates the leak cannot be located. In fact, implementing a memory leak detection tool by yourself is very simple, but I have read many examples written on the Internet and there are two common problems:

      1. Either it is not comprehensive, it can be used in one environment, but it cannot work well in another environment, or the vulnerability report output method is unreasonable.
      2. It is either too conservative. For example, there is no need to use the criticalsection before and after _ malloc_dbg () and _ free_dbg () calls (tracking the new and mallocCode).

Memory detection mainly uses the following APIs, which can track the memory applied by new and malloc methods. For details, refer to the help documentation:

 
Struct_ Crtmemstate;

_ Crtsetdbgflag ();
_ Crtmemcheckpoint ();
_ Crtmemcheckpoint ();
_ Crtmemdifference ();
_ Crtmemdumpstatistics ();
_ Malloc_dbg ();
_ Free_dbg ();

 

    • Header file: win32_crtdbg.h
 # Pragma Once

# If Defined _ debug & defined _ detect_memory_leak

# Ifdef New
# UNDEF New
# Endif

# Ifdef Delete
# UNDEF Delete
# Endif

# Ifndef _ crtdbg_map_alloc
# Define _ Crtdbg_map_alloc
# Endif

# Include <crtdbg. h>

Namespace _ Dbg_impl
{
Class Cdebugenv
{
Public :
Cdebugenv ()
{
: _ Crtsetdbgflag (_ crtdbg_alloc_mem_df | _ crtdbg_leak_check_df );
: _ Crtmemcheckpoint (& S1 );
}

~ Cdebugenv ()
{
: _ Crtmemcheckpoint (& S2 );

If (: _ Crtmemdifference (& S3, & S1, & S2 ))
{
Trace0 ( " !! Memory stats !! \ N " );
Trace0 ( " ---------------------------------------- \ N " );
: _ Crtmemdumpstatistics (& S3 );
Trace0 ( " ---------------------------------------- \ N " );
}
}

Private :
_ Crtmemstate S1, S2, S3;
};

Static _ Dbg_impl: cdebugenv _ dbgenv;
}

Inline Void * _ Cdecl Operator New (Size_t nsize, Const Char * Lpszfilename,Int Nline)
{
Return : _ Malloc_dbg (nsize, _ normal_block, lpszfilename, nline );
}

Inline Void * _ Cdecl Operator New [] (Size_t nsize, Const Char * Lpszfilename, Int Nline)
{
Return Operator New (Nsize, lpszfilename, nline );
}

Inline Void * _ Cdecl Operator New (Size_t nsize)
{
Return Operator New (Nsize, _ file __, _ line __);
}

Inline Void * _ Cdecl Operator New [] (Size_t nsize)
{
Return Operator New (Nsize, _ file __, _ line __);
}

Inline Void * _ Cdecl Operator New (Size_t nsize, Const STD: nothrow_t &)
{
Return Operator New (Nsize, _ file __, _ line __);
}

Inline Void * _ Cdecl Operator New [] (Size_t nsize, Const STD: nothrow_t &)
{
Return Operator New (Nsize, _ file __, _ line __);
}

Inline Void _ Cdecl Operator Delete ( Void * P)
{
: _ Free_dbg (p, _ normal_block );
}

Inline Void _ Cdecl Operator Delete [] ( Void * P)
{
Operator Delete (P );
}

Inline Void _ Cdecl Operator Delete ( Void * P, Const Char * Lpszfilename, Int Nline)
{
Operator Delete (P );
}

Inline Void _ Cdecl Operator Delete [] ( Void * P, Const Char * Lpszfilename, Int Nline)
{
Operator Delete (P );
}

Inline Void _ Cdecl Operator Delete ( Void * P, Const STD: nothrow_t &)
{
Operator Delete (P );
}

Inline Void _ Cdecl Operator Delete [] ( Void * P, Const STD: nothrow_t &)
{
Operator Delete (P );
}

# Define New (_ file __, _ line __)

# Endif // _ Debug & defined _ detect_memory_leak

 

    • Implementation file: win32_crtdbg.cpp
# Include"Stdafx. h"
# Include"Win32_crtdbg.h"

# IfDefined _ debug & defined _ detect_memory_leak

_ Dbg_impl: cdebugenv _ dbgenv;

# Endif //_ Debug & defined _ detect_memory_leak

 

    • Usage
      1. In stdafx. h or other public header files:# Define_ Detect_memory_leak,# Include"Win32_crtdbg.h".
      2. Delete the automatically generated new operator redefinition in the Project template. Generally, the automatically generated CPP file will redefine new as debug_new In the debug environment.
    • Problems

For the heap memory pointed to by some global variables, if ~ If cdebugenv () is not released when it is called, a false positive may occur. This is an old and difficult problem, and there is no perfect solution yet.

Codeproject

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.