Hello,
I want to check that all my memory was freed ok in Visual Studio 2008 in C++. I heard that I can add few includes and maybe write some code line and that should do it.
Does anyone know how I can do it?
Thanks in advance,
Greg
Hello,
I want to check that all my memory was freed ok in Visual Studio 2008 in C++. I heard that I can add few includes and maybe write some code line and that should do it.
Does anyone know how I can do it?
Thanks in advance,
Greg
Something like this may be what you are looking for.
#define _CRTDBG_MAP_ALLOC
#include <stdio.h>
#include <crtdbg.h>
int main()
{
malloc(100);
_CrtDumpMemoryLeaks();
return 1;
}
One of the oldest method is to override the new and delete operators (assuming all heap allocations are done through new). Prnt outs placed stratigically inside your over loaded new and delete operators let you help make sure that you have cleanedup before you exit. But this can be a bit tricky; you would be tempted to overload new and delete in global scope. But this could pose other un explained bugs.(ofcourse you would be using only overloaded operators in the debug mode, ok) Keep it to your class scope.
Another smart way is to use auto_ptr. The pointer you want to make sure you deleted is stored in an object of auto_ptr template on the stack. So when this object goes out of the scope it takes the pointer stored along with it.
If you still worried about leaks, try learning using windbg. Its an awesome tool that help you identify memory leaks. I found it really efficient to find out leaks caused by handles.
I was looking for a similar tool for VC++ 2003 and came across MemoryChecker by Software Verify (www.softwareverify.com). Not free, but very reasonably priced (less than Purify and Bounds Checker). I'm still evaluating it, but the parts I've used look very impressive. Free 30 day trial.
You can track memory errors (delete instead of delete[] etc), leaked memory, memory hotspots etc ... not having used anything like this before, I'm finding it to be pretty cool :)
There is a nice (free) utility class on CodeProject that can help with Memory Leak Detection. I found it very useful when working with some legacy code that was full of memory leaks.
To avoid leaks in new code using auto_ptr and RAAI in general is the way to go.