Memory leaks are a problem that is often seen in programming, and I have encountered two reasons:
1. Forget to recycle after allocating memory
2. There is a problem with the code, which makes it impossible to recycle, for example: int * p = new int;
p = new int; P pointer modification, the original application memory address is not recorded, so can not be released
The following describes how to check for memory leaks:
1. Include header files and definitions #define _CRTDBG_MAP_ALLOC//is not absolutely necessary, but if you have the statement, print out more intuitive information such as file name and line number
#include < stdlib.h >
#include < crtdbg.h >
(1) #include语句必须采用上文所示顺序. If you change the order, the function you use may not work correctly
(2) If there is a CPP file can not see these three lines, the following function is invalid, so you should put the three lines in a header file, make sure that each CPP file will call to it
2. Method One: Use _CrtDumpMemoryLeaks () int main (int argc, char * argv[])
{
{new int;}
_CrtDumpMemoryLeaks ();
return 0;
}
Output
Detected memory leaks!
Dumping Objects->
{{} normal block at 0x00384da8, 4 bytes long.
Data: < > CD CD CD
Object dump complete.
The contents include: Memory allocation model (in curly braces), block type (normal, client or CRT), memory location in 16, block size in bytes, block size in bytes, contents of Top 16 bytes (hexadecimal)
Attention:
(1) The position of the brace, if not added {new int;}, the memory is not leaked until the main function is finished, and _CrtDumpMemoryLeaks () is called in the main function, and then the memory leak class A is judged.
{
Public:
int * DATA;
A ()
{
Data = new int;
}
~ A ()
{
Delete Data;
}
};
int main (int argc, char * argv[])
{
A Test;
_CrtDumpMemoryLeaks ();
return 0;
}
Output
Detected memory leaks!
Dumping Objects->
{{} normal block at 0x00384da8, 4 bytes long.
Data: < > CD CD CD
Object dump complete.
(2) for some global functions, if the initialization of the application of memory, to the end of the program to release, the function will be the new application of memory as a leak to treat A Test;
int main (int argc, char * argv[])
{
_CrtDumpMemoryLeaks ();
return 0;
}
Output
Dumping Objects->
{{} normal block at 0x00384da8, 4 bytes long.
Data: < > CD CD CD
Object dump complete.
2. Method Two: Write several statements at the program entrance, when the program exits, if there is a memory leak, will automatically output in the Debug Output window and DebugView memory leak information int tmpflag = _CrtSetDbgFlag (_crtdbg_report_ FLAG);
Tmpflag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag (Tmpflag);
3. Method Three: Using _CrtMemCheckpoint (), you can detect the memory leak of a program segment int main (int argc, char * argv[])
{
Crtmemstate S1, S2, S3;
_CrtMemCheckpoint (& S1);
new int; Program Segment
_CrtMemCheckpoint (& S2);
if (_CrtMemDifference (& S3, & S1, & S2)) _CrtMemDumpStatistics (& S3);
return 0;
}
Output
Bytes in 0 free Blocks.
4 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:0 bytes.
Total Allocations:4 bytes.