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.