Memory Leaks - ProkopHapala/FireCore GitHub Wiki
- Basic method of debugging memory leaks is to use ASan which is possible to with on in
CMake
bycmake .. -DWITH_ASAN=ON
- This will only inform that some chunk of memory was not deallocated at the end of program and address to that chunk of memory
- To get better information about where these chunks of memory were allocated we can overload
new []
anddelete []
operators for arrays.- We do this in debugAllocator.h and macroUtils.h
How it works
class DebugAllocator
store records of allocated memory and string caption of where they were allocated.- then we redefine allocation macros which log file and line number of
#define _CODE_LOCATION __FILE__ ":" _TOSTRING(__LINE__)
#define _new(T,n) debug_alloc<T>(debugAllocator,n,_CODE_LOCATION ) `
#define _realloc(arr,n) debug_alloc_store( __realloc(arr,n), n, _CODE_LOCATION )
#define _realloc0(arr,n,v0) debug_alloc_store( __realloc0(arr,n,v0), n, _CODE_LOCATION )
Example how to use it:
- To use debug allocator we need to make sure that macro flag
DEBUG_ALLOCATOR
is define by#define DEBUG_ALLOCATOR
- We need to initialize the static global instance of
DebugAllocator* debugAllocator
by callingdebugAllocator_init();
att he begining of the program:
int main(int argc, char *argv[]){
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
glEnable(GL_MULTISAMPLE);
//SDL_SetRelativeMouseMode( SDL_TRUE );
int junk;
SDL_DisplayMode DM;
SDL_GetCurrentDisplayMode(0, &DM);
debugAllocator_init();
// --------- Initialize MolGUI and MolWorld_sp3
W = new MolWorld_sp3();
app = new MolGUI( junk, DM.w-100, DM.h-100, W );
- before we exit the program we print content of the debugAllocator
W->pre_loop();
app->loop( 1000000 );
debugAllocator_print( );
return 0;
}
The output is like this:
DebugAllocator::print_allocated()
[0] ptr @ 0x7c1874001800 n_items: 20480000 item_size: 16 allocated at /home/prokop/git/FireCore/cpp/common/molecular/GridFF.h:185
[1] ptr @ 0x7c1860601800 n_items: 20480000 item_size: 16 allocated at /home/prokop/git/FireCore/cpp/common/molecular/GridFF.h:186
[2] ptr @ 0x7c184cc01800 n_items: 20480000 item_size: 16 allocated at /home/prokop/git/FireCore/cpp/common/molecular/GridFF.h:187
[3] ptr @ 0x7c1825a01800 n_items: 20480000 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/GridFF.h:190
[4] ptr @ 0x7c17fe801800 n_items: 20480000 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/GridFF.h:191
[5] ptr @ 0x7c17d7601800 n_items: 20480000 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/GridFF.h:192
[6] ptr @ 0x50300000e320 n_items: 1 item_size: 24 allocated at /home/prokop/git/FireCore/cpp/common/molecular/NBFF.h:1096
[7] ptr @ 0x51c000038880 n_items: 240 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:142
[8] ptr @ 0x51c000039080 n_items: 240 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:143
[9] ptr @ 0x51e000006080 n_items: 120 item_size: 24 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:149
[10] ptr @ 0x51e000006c80 n_items: 120 item_size: 24 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:150
[11] ptr @ 0x5110000160c0 n_items: 50 item_size: 4 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:152
[12] ptr @ 0x51800002f880 n_items: 50 item_size: 16 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:153
[13] ptr @ 0x51800002f480 n_items: 50 item_size: 16 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:154
[14] ptr @ 0x51800002f080 n_items: 50 item_size: 16 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:155
[15] ptr @ 0x51900001f480 n_items: 30 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:156
[16] ptr @ 0x51900001f980 n_items: 30 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:157
[17] ptr @ 0x51900001fe80 n_items: 30 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:158
[18] ptr @ 0x519000020380 n_items: 30 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:159
[19] ptr @ 0x519000020880 n_items: 30 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:160
[20] ptr @ 0x5210000c6d00 n_items: 180 item_size: 24 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:164
[21] ptr @ 0x502000055a10 n_items: 0 item_size: 16 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:166
[22] ptr @ 0x502000056bb0 n_items: 0 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:167
[23] ptr @ 0x51b00000b680 n_items: 50 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:169
[24] ptr @ 0x51b00000bd80 n_items: 50 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3_loc.h:170
[25] ptr @ 0x51c00003a080 n_items: 234 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:82
[26] ptr @ 0x51c00003a880 n_items: 234 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:83
[27] ptr @ 0x511000016340 n_items: 50 item_size: 4 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:86
[28] ptr @ 0x514000001e40 n_items: 56 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:92
[29] ptr @ 0x514000002040 n_items: 56 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:93
[30] ptr @ 0x514000002240 n_items: 56 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:94
[31] ptr @ 0x514000002440 n_items: 56 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:95
[32] ptr @ 0x51500000c100 n_items: 120 item_size: 4 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:101
[33] ptr @ 0x515000006e80 n_items: 120 item_size: 4 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:102
[34] ptr @ 0x519000020d80 n_items: 30 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:104
[35] ptr @ 0x51a000021080 n_items: 56 item_size: 24 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MMFFsp3.h:135
[36] ptr @ 0x50300000e9e0 n_items: 1 item_size: 24 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MolWorld_sp3.h:750
[37] ptr @ 0x51800002ec80 n_items: 50 item_size: 16 allocated at /home/prokop/git/FireCore/cpp/common/molecular/NBFF.h:176
[38] ptr @ 0x51b00000cb80 n_items: 50 item_size: 32 allocated at /home/prokop/git/FireCore/cpp/common/molecular/NBFF.h:189
[39] ptr @ 0x51c00003b080 n_items: 240 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/math/DynamicOpt.h:162
[40] ptr @ 0x51c00003b880 n_items: 240 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/math/DynamicOpt.h:164
[41] ptr @ 0x51c00003c080 n_items: 240 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/math/DynamicOpt.h:167
[42] ptr @ 0x51c00003c880 n_items: 240 item_size: 8 allocated at /home/prokop/git/FireCore/cpp/common/math/DynamicOpt.h:168
[43] ptr @ 0x511000016480 n_items: 50 item_size: 4 allocated at /home/prokop/git/FireCore/cpp/common/molecular/MolWorld_sp3.h:1431