Search code examples
c++debuggingvisual-studio-2010memory-leaksmsvcrt

Breaking when a certain amount of bytes is allocated


_CrtDumpMemoryLeaks(); if you didn't know, is a function that dumps all the memory leaks in a program. Mine currently displays that I have a 3632062 byte memory leak (it's not being deallocated).

I was wondering: Is there any way to cause Visual C++ Express to break when a certain amount of bytes has been allocated? That way I could break when 3632062 bytes have been allocated, then read the stack trace to see where I allocated it.

This is currently the only method I can think of for finding where the memory is being allocated so I can fix it. I've been searching my code a lot but I can't find anywhere where I would need to allocate 3632062 bytes (since the only file I load is 2767136 bytes..) although I am certain the leak is related to the file I'm operating on.

Any ideas for finding the source of the memory leak? I'm using Native C++, Visual C++ 2010


Solution

  • You could do this using _CrtSetAllocHook to track the total memory usage.

    UMDH will give you a list of allocated blocks in all heaps. This might be what you want, since breaking on hitting a given total alloc threshold will not tell you where all of the blocks were allocated.