Another technique for locating a memory leak involves taking a snapshot of the memory state of the application at a critical point. To take a snapshot of the memory state given to a point in the application, create a _crtmemstate structure and pass it to the _CrtMemCheckpoint function:
_CrtMemState S1;_crtmemcheckpoint (&S1);
_CrtMemCheckpoint will populate the structure with the current memory state.
If you want to see the contents of the output _crtmemstate structure, you can use the _ Crtmemdumpstatistics function:
_CrtMemDumpStatistics (&S1);//_ crtmemdumpstatistics output memory state dump as follows://0 bytes in 0 free blocks.//0 bytes in 0 Normal blocks.//3071 bytes in + CRT blocks.//0 bytes in 0 Ignore blocks.//0 bytes in 0 Client blocks.//largest number used: 3071 bytes.//Total allocations:3764 bytes.
To determine if a memory leak has occurred in a section of code, you can take a snapshot of the memory state before and after this section, and then use _ Crtmemdifference to compare two states:
_CrtMemCheckpoint (&S1);//memory allocations take place Here_crtmemcheckpoint (&S2); if (_CrtMemDifference (&am P;S3, &S1, &s2)) {_crtmemdumpstatistics (&S3);}
_CrtMemDifference compares the memory states S1 and S2, returning results in (S3), that is, the difference between S1 and S2.
One way to look for a memory leak is to first place the _CrtMemCheckpoint call at the beginning and end of the application, and then use _CrtMemDifference to compare the two results. If _CrtMemDifference shows a memory leak, add more _crtmemcheckpoint to use binary search to divide the program until a leak source is found.