How to compare states of memory in memory leak?
I have recently started studying about the memory leak. And also found that some problems in uTorrent are caused because of memory leaks. Now I wanted to know how can I compare states of memory in memory leak? Since, I am not knowing much about it, please explain me in details. Hoping that someone hanging out there will help me soon.!! :rolleyes:
Re: How to compare states of memory in memory leak?
To conclude whether a memory leak took place in a section of code, you can take snapshots of the state of memory prior to and after the section, and compare the two states with _CrtMemDifference :
Code:
_CrtMemCheckpoint (& S1);
/ / Memory allocations take place here
_CrtMemCheckpoint (& S2);
if (_CrtMemDifference (& s3, & s1, & s2))
_CrtMemDumpStatistics (& S3);
Re: How to compare states of memory in memory leak?
Another modus operandi for detecting memory leaks is to take snapshots of the state of the application memory in certain key points. The CRT provides a type of structure, _CrtMemState, which you can use to store a snapshot of the memory :
_CrtMemState S1, s2, s3;
Re: How to compare states of memory in memory leak?
To take a snap of the state of memory at a prearranged point, pass a structure to function _CrtMemState _CrtMemCheckpoint . This function fills the arrangement with a snapshot of the current state of memory :
- _CrtMemCheckpoint (& S1);
You can dump the contents of a structure _CrtMemState at any point in passing the structure to function _CrtMemDumpStatistics :
- _CrtMemDumpStatistics (& S1);
Re: How to compare states of memory in memory leak?
As its name suggests, _CrtMemDifference compares two memory states (s1 and s2) and produces a result (s3), which corresponds to the difference between the two states. By placing calls to _CrtMemCheckpoint at the beginning and the end of your program, then using _CrtMemDifference to compare the results, you have another way to search for memory leaks. If a leak is detected, you can use calls to _CrtMemCheckpoint to split your program and locate the leak with a binary search technique.