My environment is: XP SP2. VS2003
Recently in a project, a memory leak occurs after the program exits:
Detected memory leaks! Dumping Objects->{98500} normal block at 0x05785ad0, bytes long. Data: << n n x 7 > 3C AC 4E, BC A4 4E B6 PNs 00Object dump complete.
And each exit is the same. The leaked memory blocks are all 98500.
Workaround:
1. Use the code where the program starts (before it is enough, just before the leaking memory allocation):
_CrtSetBreakAlloc (98500); 98500 is the block number for the memory leak above.
2. Then debug run, the program automatic breakpoint in "Memory block 98500" Allocation location:
/* Break to debugger at specific memory allocation */
if (_crtbreakalloc! = -1l && lrequest = = _crtbreakalloc) _crtdbgbreak ();
3. We have found the place where the underlying operation of the memory allocation is leaking (good mouthful!).
This time we use the "debug" –> "Exit", the shortcut is: "Shift + F11". (Directly looking at the stack faster)
Jump out of the current function: Then go up to the "up" and jump to see the call stack. Until you see the code that you wrote, like my
std::ifstream* Origstream = new Std::ifstream ();
4. At this point I can already tell that origstream allocated memory, but there is no reasonable release. The memory leak does not occur when the process is run again.
5. Finally, remember to put _CrtSetBreakAlloc (98500); This sentence deleted, or every time the breakpoint, is not bored to death.
----------------------------------------------------------------------------------------------------------
Write in the back:
About memory leak detection, there is a visual Leak Detector, according to its introduction is really good, can be directly specific to the line of code to indicate a memory leak, but the personal use, found that the speed of the debug is too slow, do not know is not my personal program problems .... Like the leak above, I use Crtsetbreakalloc this simple method is enough ....
By: Hong Gengwei
Transferred from:http://www.cnblogs.com/G_Weber/archive/2009/04/15/1436362.html
http://blog.csdn.net/qing666888/article/details/50263763
_CrtSetBreakAlloc simple memory leak detection method, solve detected memory leaks! problem