1. Non-MFC programs can use the following methods to detect memory leaks:
1. The program is defined as follows:
# Ifdef _ debug
# Define debug_clientblock new (_ client_block, _ file __, _ line __)
# Else
# Define debug_clientblock
# Endif // _ debug
# DEFINE _ crtdbg_map_alloc
# Include
# Include
# Ifdef _ debug
# Define new debug_clientblock
# Endif // _ debug
2. Add the following functions to the program:
_ Crtsetdbgflag (_ crtdbg_alloc_mem_df | _ crtdbg_leak_check_df );
If memory leakage occurs after the debug version is run, the output window displays similar information:
Detected memory leaks!
Dumping objects-> G: \ Programs \ test. cpp (16 ):
{51} client block at 0x000000c58, subtype 0, 4 bytes long.
Data: <> CD object dump complete.
Ii. MFC program memory leakage detection method:
1. Add the following three member variables of the cmemorystate class to the cmyapp:
# Ifdef _ debug
Protected: cmemorystate m_msold, m_msnew, m_msdiff;
# Endif // _ debug
2. Add the following code to cmyapp: initinstance:
# Ifdef _ debug
M_msold.checkpoint ();
# Endif // _ debug
3. Add the following code to cmyapp: exitinstance:
# Ifdef _ debug
M_msnew.checkpoint ();
If (m_msdiff.difference (m_msold, m_msnew ))
{
Afxdump <"\ nmemory leaked: \ n ";
M_msdiff.dumpstatistics ();
Afxdump <"Dump complete! \ N ";
}
# Endif // _ debug
If memory leakage occurs after the debug version is run, the output window displays similar information:
Memory leaked:
0 bytes in 0 free blocks. 8 bytes in 1 normal blocks.
0 bytes in 0 CRT blocks. 0 bytes in 0 ignore blocks.
0 bytes in 0 client blocks. largest number used: 8825 bytes.
Total allocations: 47506 bytes.
Dump complete!
Detected memory leaks!
Dumping objects-> G: \ Programs \ chat \ chatdlg. cpp (120 ):
{118} normal block at 0x00d98150, 8 bytes long.
Data: <> A8 7f D9 00 01 00 00 00 00 object dump complete.
Vs2005 C ++ Memory leakage detection classification: VC ++ 810 people read comments (1) collect reports C ++ Delete compiler object development tool leak
Development Tool: vs2005. (It seems that this can be used in VC ++ 6.0)
Objective: To check whether memory leakage exists in the C ++ code (that is, the memory is dynamically allocated but not correctly released ).
Procedure:
1. Add the following debug_new.h and debug_new.cpp files to the project.
Debug_new.h
[CPP]View plaincopy
- # Ifndef _ debug_new_h _
- # DEFINE _ debug_new_h _
- # Ifdef _ debug
- # UNDEF new
- Extern void _ regdebugnew (void );
- Extern void * _ cdecl operator new (size_t, const char *, INT );
- Extern void _ cdecl operator Delete (void *, const char *, INT );
- # Define new (_ file __, _ line __)
- # Define reg_debug_new _ regdebugnew ();
- # Else
- # Define reg_debug_new
- # Endif // _ debug
- # Endif/_ debug_new_h _
Debug_new.cpp
[C-sharp]View plaincopy
- // # Include "debug_new.h"
-
- # Ifdef _ debug
-
- # Include
- # Include
- Class _ criec
- {
- Critical_section criection;
- Public:
- _ Criec () {initializecriticalsection (& criection );}
- ~ _ Criec () {deletecriticalsection (& criection );}
- Void enter () {entercriticalsection (& criection );}
- Void leave () {leavecriticalsection (& criection );}
- } _ Cs;
- Void _ regdebugnew (void)
- {
- _ Crtsetdbgflag (_ crtdbg_report_flag | _ crtdbg_leak_check_df );
- }
- Void * _ cdecl operator new (size_t nsize, const char * lpszfilename, int nline)
- {
- // Comment 1: although the debug new provided by MFC is locked, I found the multi-thread concurrency during actual testing.
- // A system error is thrown during the call, so I add a thread mutex here.
- // Comment 2: mutual exclusion between debug new and debug Delete is not required. I am not sure. For the sake of insurance, I am the same
- // Adds the thread mutex.
- // Comment 3: According to the C ++ standard, the set_new_handler setting should be called after operator new fails.
- // Function, but msdn says, "The set_new_handler in the header file new is stub, instead
- // Use the _ set_new_handler in the header file new. h.
- // The following is the set_new_handler definition in VC ++ 6.0:
- // New_handler _ cdecl set_new_handler (new_handler new_p)
- //{
- // Assert (new_p = 0); // cannot use stub to register a new handler
- // _ Set_new_handler (0 );
- // Return 0;
- //}
- // So I have no choice but to discard the role of set_new_handler.
- _ CS. Enter ();
- Void * P = _ malloc_dbg (nsize, _ normal_block, lpszfilename, nline );
- _ CS. Leave ();
- Return P;
- }
- Void _ cdecl operator Delete (void * P, const char *, INT)
- {
- _ CS. Enter ();
- _ Free_dbg (p, _ client_block );
- _ CS. Leave ();
- }
-
- # Endif
2. Add dynamicmem. cpp to be detected
1.
# Include "debug_new.h ".
2.
Use the debug heap function:
# DEFINE _ crtdbg_map_alloc
# Include
# Include
Note: # include statement order. If you change this order, the function used may not work properly.
3. Add the reg_debug_new macro to the beginning of main ().
4. Add the following statement to check for Memory leakage to output Memory leakage information:
_ Crtdumpmemoryleaks ();
Test the instance program:
Dynamicmem. cpp
[CPP]View plaincopy
- # Include
- # Include
- # Include "debug_new.h" // +
- # DEFINE _ crtdbg_map_alloc
- # Include
- # Include
- Using namespace STD;
- Int main ()
- {
- Reg_debug_new; // +
- Char * name = new char [2];
- Name [0] = 'a ';
- Name [1] = 'B ';
- // Delete name;
- _ Crtdumpmemoryleaks ();
- Cout <"-- end --" <Endl;
- Return 0;
- }
3. Press F5 to run.
It will be displayed in the "debug" window:
Detected memory leaks!
Dumping objects->
E:/workspaces/C ++/dynamicmem. cpp (78): {120} normal block at 0x003b6360, 2 bytes long.
Data: 41 42
Object dump complete.
This indicates Memory leakage.
For example, if Delete [] Name; is removed, the above information will not appear in the "debug" window, indicating that there is no memory leakage.
Reference: http://blog.sina.com.cn/s/blog_51396f890102f96e.html