1. Use _ crtdumpmemoryleaks () to check whether there is memory leak in program.
With the help of _ crtdbg_map_alloc, it can output Memory Leak info with file line info for those memory block allocated by malloc (), while it wowould never print file line info for those allocated by new ().
#define _CRTDBG_MAP_ALLOC#include <stdlib.h>
#include <crtdbg.h>
Call
{
_ Crtdumpmemoryleaks ();
}
At the end of the program exit if there is only one exit in the program.
Or
Call
{
_ Crtsetdbgflag (_ crtdbg_alloc_mem_df | _ crtdbg_leak_check_df );
}
At the beginning of your program to ensure _ crtdumpmemoryleaks () wocould be called when the program exits from any exit point.
2. Use debug_new method to check in detail
# Define debug_new new (_ normal_block, this_file, _ line __)
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif
{
_ Crtmemdumpallobjectssince (null );
}
3. Use tool like leakfinder (here: http://www.codeproject.com/KB/applications/leakfinder.aspx)
I still prefer to use shared_ptr to manage heap allocation instead of putting too much effort on detecting memory leak.
Keep on learning.