Question

I am following msdn article for finding memory leaks using CRT.

http://msdn.microsoft.com/en-us/library/x98tx3cf%28v=vs.100%29.aspx

I added _CrtDumpMemoryLeaks(); to the exit point of my application. It shows me thousands of memory leaks in different files. But I am interested in finding memory leaks of a particular file/class/function. Is there any possible way to implement this.

Here is what I tried to do.

void SomeClass::SomeRandomFunction(SomeRandomParameters)
{
    _CrtDumpMemoryLeaks();                 // Start of function.
    // Some lines of codes which may contain memory leaks.

    _CrtDumpMemoryLeaks();                 // End of function.
}

I added breakpoints on entry and exit of this method. I thought that second DumpMemory function will display only memory leaks which were find between these two DumpMemory function calls. But it didn't happened. Is there any other way to do this?

Was it helpful?

Solution

_CrtDumpMemoryLeaks() should only ever be used at the end of the program. What you are looking for is _CrtMemCheckpoint(), call it at the start of the function to take a snapshot. And use _CrtMemDumpAllObjectsSince() at the end of the function to see what's was allocated since the snapshot but not released. Be careful, they might not necessarily be leaks when you make it this fine-grained.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top