1. Add in the header
1.1 Method 1:
# Ifdef _ debug <br/> # define debug_malloc (s) _ malloc_dbg (S, _ normal_block, _ file __, _ line __) <br/> # define malloc (s) debug_malloc (s) <br/> # include <crtdbg. h> <br/> # define debug_new new (_ normal_block, _ file __, _ line __) <br/> # define new debug_new <br/> # endif <br/> // _ file path <br/> // _ line _ row number
1.2 Method 2:
# Ifdef _ debug <br/>/* <br/> _ crtdbg_map_alloc must be included in # include <crtdbg. h> previously defined <br/> but defining _ crtdbg_map_alloc in the Code does not work. The following definition is required: <br/> // # define debug_malloc (s) _ malloc_dbg (S, _ normal_block, _ file __, _ line _) <br/> // # define malloc (s) debug_malloc (s) <br/> therefore, it is best to define _ crtdbg_map_alloc directly in Preprocessor. <Br/> */<br/> // # DEFINE _ crtdbg_map_alloc <br/> # include <crtdbg. h> <br/> # define debug_new (_ normal_block, _ file __, _ line _) <br/> # define new debug_new <br/> # endif
2. Add
_ Crtsetdbgflag (_ crtdbg_alloc_mem_df | _ crtdbg_leak_check_df); <br/> _ crtsetbreakalloc (91 ); <br/> // _ crtsetdbgflag is added, and the memory leakage detection is performed. <br/> // _ crtsetbreakalloc is added to this sentence, and the breakpoint is broken to 91 lines at the leak location, the program is no longer running. Without this sentence, the program runs and the output file line number is equal to the leaked information. <Br/>