Memory leakage check

Source: Internet
Author: User

 

InThe memory check mechanism provided by VC is to track new operations, that is, all new operations will be recorded, if the memory allocated through the new operation is not normally deleted, the system will display a specific memory leakage information in the debugging window when the program exits.

SimilarlyThe memory allocated by malloc will also be tracked, but it will not know where the malloc operation is performed in the real program when it is displayed. First, let's look at the following example in the pipeline:

Void _ tmain ()
{...
Char * pszNew = (char *) malloc (200 );
Char * pszNew2 = new char [2, 100];
CString * pszNew3 = new CString ("test ");
...
}

// Run and exit after debugging. You can see the following information about memory leakage in the debugging information:

Detected memory leaks!
Dumping objects->
Strcore. cpp (118): {37} normal block at 0x007702E0, 17 bytes long.
Data: <test> 01 00 00 00 00 00 00 00 00 00 00 74 65 73 74
G: emp2sam_sp_33sam_sp_33.cpp (42): {36} normal block at 0x00770520, 4 bytes long.
Data: <w> EC 02 77 00
// Information generated for CString * pszNew3 = new CString ("test ");
G: emp2sam_sp_33sam_sp_33.cpp (41): {35} normal block at 0x00770320,100 bytes long.
Data: <> CD
// Information generated for char * pszNew2 = new char [100 ];
{34} normal blocks at 0x007703B0, 200 bytes long.
Data: <> CD
// Information generated by char * pszNew = (char *) malloc (200 );
Object dump complete.

You can see thatNew operations performed on the row of the file will be reported when the information is displayed in the new allocation, the memory allocated through malloc only displays Memory leakage information and cannot locate the program location for memory allocation.

In addition, if you need to define in the file headerOnly the DEBUG_NEW macro can correctly track new operations. The Code is as follows:

# Ifdef _ DEBUG
  
# Define new DEBUG_NEW
  
# Endif
  

BecauseThe new operation trace only needs to appear in the debugging version, so Conditional compilation is used.

We can see thatThe method provided by VC to check memory leakage is very easy to use. We must pay attention to the memory allocation problem during program development, especially for some long-running programs.
 

 

Related Article

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.