I'm using the CMemoryState to check for memory leaks as follows:
CMemoryState oldMemState, newMemState, diffMemState;
if( diffMemState.Difference( oldMemState, newMemState ) )
TRACE("Memory leaked!\n" );
AfxMessageBox(L"Memory leaked!\n" );
Can somebody tell me how the struct is used to check for leaks. I have been looking at the members as I do my check and it seems that the only member in diffMemState that indicates a memory leak when it is non-zero is m_lCounts. If any of the other m_lCounts are non-zero, it doesn't seem to mean that there is a memory leak - is that correct, and of so what do they mean?
14 Dec '11 - 10:44
There is another useful call to add to your code,
diffstate.DumpStatistics(); That should tell you of the things that remain allocated between the first checkpoint and the second checkpoint.
Now from previous notes, you should realize that not all "still allocated" objects / memory are "leaks". It might simply be some object that you are still using. For example, my application caches data read from a file. If I were to take the checkpoint before reading the data and then check for "leaks", the cached data would appear as "still allocated". Is that a "leak", no because I know about it and it was part of the design.
So use the "DumpStatistics" after your TRACE message to see what memory is still allocated. If you have the default DEBUG_NEW allocation, you should also see the module and line number where that memory was allocated. You might even be able to double-click on the output and Visual Studio should take you to the line where the allocation / new occurred.
14 Dec '11 - 11:21
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)