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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With