Vs2005 Memory leakage detection method [Reproduced]

Source: Internet
Author: User

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
  1. # Ifndef _ debug_new_h _
  2. # DEFINE _ debug_new_h _
  3. # Ifdef _ debug
  4. # UNDEF new
  5. Extern void _ regdebugnew (void );
  6. Extern void * _ cdecl operator new (size_t, const char *, INT );
  7. Extern void _ cdecl operator Delete (void *, const char *, INT );
  8. # Define new (_ file __, _ line __)
  9. # Define reg_debug_new _ regdebugnew ();
  10. # Else
  11. # Define reg_debug_new
  12. # Endif // _ debug
  13. # Endif/_ debug_new_h _

 

Debug_new.cpp

 

 

[C-sharp]View plaincopy
  1. // # Include "debug_new.h"
  2.  
  3. # Ifdef _ debug
  4.  
  5. # Include
  6. # Include
  7. Class _ criec
  8. {
  9. Critical_section criection;
  10. Public:
  11. _ Criec () {initializecriticalsection (& criection );}
  12. ~ _ Criec () {deletecriticalsection (& criection );}
  13. Void enter () {entercriticalsection (& criection );}
  14. Void leave () {leavecriticalsection (& criection );}
  15. } _ Cs;
  16. Void _ regdebugnew (void)
  17. {
  18. _ Crtsetdbgflag (_ crtdbg_report_flag | _ crtdbg_leak_check_df );
  19. }
  20. Void * _ cdecl operator new (size_t nsize, const char * lpszfilename, int nline)
  21. {
  22. // Comment 1: although the debug new provided by MFC is locked, I found the multi-thread concurrency during actual testing.
  23. // A system error is thrown during the call, so I add a thread mutex here.
  24. // 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
  25. // Adds the thread mutex.
  26. // Comment 3: According to the C ++ standard, the set_new_handler setting should be called after operator new fails.
  27. // Function, but msdn says, "The set_new_handler in the header file new is stub, instead
  28. // Use the _ set_new_handler in the header file new. h.
  29. // The following is the set_new_handler definition in VC ++ 6.0:
  30. // New_handler _ cdecl set_new_handler (new_handler new_p)
  31. //{
  32. // Assert (new_p = 0); // cannot use stub to register a new handler
  33. // _ Set_new_handler (0 );
  34. // Return 0;
  35. //}
  36. // So I have no choice but to discard the role of set_new_handler.
  37. _ CS. Enter ();
  38. Void * P = _ malloc_dbg (nsize, _ normal_block, lpszfilename, nline );
  39. _ CS. Leave ();
  40. Return P;
  41. }
  42. Void _ cdecl operator Delete (void * P, const char *, INT)
  43. {
  44. _ CS. Enter ();
  45. _ Free_dbg (p, _ client_block );
  46. _ CS. Leave ();
  47. }
  48.  
  49. # 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
  1. # Include
  2. # Include
  3. # Include "debug_new.h" // +
  4. # DEFINE _ crtdbg_map_alloc
  5. # Include
  6. # Include
  7. Using namespace STD;
  8. Int main ()
  9. {
  10. Reg_debug_new; // +
  11. Char * name = new char [2];
  12. Name [0] = 'a ';
  13. Name [1] = 'B ';
  14. // Delete name;
  15. _ Crtdumpmemoryleaks ();
  16. Cout <"-- end --" <Endl;
  17. Return 0;
  18. }

 

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

 

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.