c++ - How to override general malloc so I get line number printed in memory leak printout -


i have written utility code detect memory leaks when program closes. detect_leaks.hpp , cpp contain function sets things , in program call start_detecting() function @ program start.

note code uses microsoft debug functions applicable windows.

my report prints out line in file leaked allocation new because have this:

#define new new(_client_block,__file__, __line__) 

but how same thing malloc? or don't mind changing name eg mmalloc if required. yes know shouldn't using malloc, have old code.

here code far:

detect_leaks.hpp:

#ifndef __detect_leaks_hpp__ #define __detect_leaks_hpp__  #include <stdio.h> #include <string.h> #include <malloc.h> #include <crtdbg.h>  #define new new(_client_block,__file__, __line__)  // following macros set , clear, respectively, given bits // of c runtime library debug flag, specified bitmask. #ifdef   _debug #define  set_crt_debug_field(a) \     _crtsetdbgflag((a) | _crtsetdbgflag(_crtdbg_report_flag)) #define  clear_crt_debug_field(a) \     _crtsetdbgflag(~(a) & _crtsetdbgflag(_crtdbg_report_flag)) #else #define  set_crt_debug_field(a)   ((void) 0) #define  clear_crt_debug_field(a) ((void) 0) #endif  void start_detecting();  #endif // __detect_leaks_hpp__ 

detect_leaks.cpp:

#include "detect_leaks.hpp"  void start_detecting() {     // send reports stdout     _crtsetreportmode( _crt_warn, _crtdbg_mode_file );     _crtsetreportfile( _crt_warn, _crtdbg_file_stdout );     _crtsetreportmode( _crt_error, _crtdbg_mode_file );     _crtsetreportfile( _crt_error, _crtdbg_file_stdout );     _crtsetreportmode( _crt_assert, _crtdbg_mode_file );     _crtsetreportfile( _crt_assert, _crtdbg_file_stdout );      // set debug-heap flag freed blocks kept on     // linked list, catch inadvertent use of freed memory     set_crt_debug_field( _crtdbg_delay_free_mem_df );      // set debug-heap flag memory leaks reported when process terminates.     set_crt_debug_field( _crtdbg_leak_check_df ); } 

main.cpp (sample usage):

#include "detect_leaks.hpp"  int  main() {     start_detecting();      char* s = (char*)malloc(100);     strcpy(s, "abc leak1");      char* s1 = new char[100]();     strcpy(s1, "abc leak2");      return 0; } 

sample printout:

detected memory leaks! dumping objects -> \memleak_malloc\main.cpp(10) : {74} client block @ 0x00161a90, subtype 0, 100 bytes long.  data: <abc leak2       > 41 42 43 20 6c 65 61 6b 32 00 00 00 00 00 00 00 {73} normal block @ 0x00164f50, 100 bytes long.  data: <abc leak1       > 41 42 43 20 6c 65 61 6b 31 00 cd cd cd cd cd cd object dump complete. 

sorry ask. found answer question.

it quite convenient, just

#define _crtdbg_map_alloc 

then report looks this:

detected memory leaks! dumping objects -> \memleak_malloc\main.cpp(10) : {74} client block @ 0x009c1a90, subtype 0, 100 bytes long.  data: <abc leak2       > 41 42 43 20 6c 65 61 6b 32 00 00 00 00 00 00 00 \memleak_malloc\main.cpp(7) : {73} normal block @ 0x009c4f50, 100 bytes long.  data: <abc leak1       > 41 42 43 20 6c 65 61 6b 31 00 cd cd cd cd cd cd object dump complete. 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -