Finding memory leaks in C code on Windows
I already know that I can trace memory leaks in my code with mtrace
and valgrind
on Linux, both of which are unavailable for Windows. Which Windows program would you recommend to trace 开发者_如何学JAVAmemory leaks?
I'm an Eclipse user and I've been working with C for a month or two now, so I prefer a user-friendly solution over something more advanced.
Application Verifier will do this quite well, http://msdn.microsoft.com/en-us/library/ms220948.aspx
You can use _CrtDumpMemoryLeaks. I suppose it is similar to mtrace.
More info: http://www.codeguru.com/forum/showthread.php?t=312742
See Purify and possibly Insecure++
You can hook up e.g. visual leak detector as described here: http://www.codeproject.com/KB/applications/visualleakdetector.aspx
Another way would be to do count the amount of memory used before and after a specific action. Like described here: msdn.microsoft.com/en-us/library/aa293901%28VS.60%29.aspx Something like this can easily added to e.g. automatic unit tests.
There is a common sense way of doing this, in the lines of C, for every pair of malloc
there is a free
, if there isn't there's a leak, likewise the same for the GlobalAlloc
, VirtualAlloc
, HeapAlloc
, LocalAlloc
, VirtualAllocEx
...there's an associated ...Free
pair with them, for example running a HeapAlloc
on variable 'foo' for example, and there is no HeapFree
for 'foo', that is a leak...
Hope this helps, Best regards, Tom.
精彩评论