Memory leak detection... the best solution?
-
Hi world, What's the best solution to find the pointer which caused a memory leak? I don't use MFC (and doesn't want to use it)!!! Is smart pointer a good solution? Thanks for any advice... Hello World!!! :) from Raphaël
-
Hi world, What's the best solution to find the pointer which caused a memory leak? I don't use MFC (and doesn't want to use it)!!! Is smart pointer a good solution? Thanks for any advice... Hello World!!! :) from Raphaël
You can use the CRT Heap check functions:
#include "crtdbg.h" #if defined(WIN32) && defined(_DEBUG) // Track and report heap errors const int _CrtDbgFlags =_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); _CrtSetDbgFlag(_CrtDbgFlags|_CRTDBG_LEAK_CHECK_DF); #endif
I think there is a recent article here on CP about overiding new and delete to locate memory problems as well. Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program -
Hi world, What's the best solution to find the pointer which caused a memory leak? I don't use MFC (and doesn't want to use it)!!! Is smart pointer a good solution? Thanks for any advice... Hello World!!! :) from Raphaël
Hi, http://www.codeproject.com/script/comments/forums.asp?msg=491851&forumid=1647&mode=all&userid=161454#xx491851xx ----------------------------------------------------------- Hi, Regarding the technique of dumping in-memory statistics, is given below Note: memstate1 takes snapshot of pre-memory leak and memstate2 takes snapshot of post-memory leak. finally, memstate3 makes the statistics based on both the snapshots. CMemoryState memState1, memState2, memState3; void CHello::MakeMemoryLeak() { memState1.Checkpoint(); LPCTSTR strMemoryLeak = new char[50]; memState2.Checkpoint(); memState3.Difference(memState1, memState2); memState3.DumpStatistics(); } Hope this Helps Regards ~Hari~
-
Hi, http://www.codeproject.com/script/comments/forums.asp?msg=491851&forumid=1647&mode=all&userid=161454#xx491851xx ----------------------------------------------------------- Hi, Regarding the technique of dumping in-memory statistics, is given below Note: memstate1 takes snapshot of pre-memory leak and memstate2 takes snapshot of post-memory leak. finally, memstate3 makes the statistics based on both the snapshots. CMemoryState memState1, memState2, memState3; void CHello::MakeMemoryLeak() { memState1.Checkpoint(); LPCTSTR strMemoryLeak = new char[50]; memState2.Checkpoint(); memState3.Difference(memState1, memState2); memState3.DumpStatistics(); } Hope this Helps Regards ~Hari~
And without MFC...??? Hello World!!! :) from Raphaël
-
And without MFC...??? Hello World!!! :) from Raphaël
Hi, Its not MFC. Basically, it uses _CRT... Functions/Flags only. But the thing is, CMemoryState is declared & defined AFX.H/AFXMEM.cpp respectively. Just have a look at the code, if possible write your own. Hope this helps.
-
And without MFC...??? Hello World!!! :) from Raphaël
I use macros as a wrapper to the malloc, realloc and free functions. This way I can log all my memory allocations to a file, the following is a bit complicated, but works: #define realloc(PTR, SIZE) \ logReallocPtr(PTR, realloc(PTR, SIZE), ck_memsize(PTR), SIZE, __FILE__, #PTR, __LINE__, NULL) #define malloc(SIZE) \ logReallocPtr(NULL, malloc(SIZE), 0, SIZE, __FILE__, #SIZE, __LINE__, NULL) #define free(PTR) \ logReallocPtr(PTR, NULL, ck_memsize(PTR), 0, __FILE__, #PTR, __LINE__, free) // This function gets called for every malloc, realloc and free void *logReallocPtr(void *oldPtr, void *newPtr, size_t oldSize, size_t newSize, const char *file, const char *func, size_t line, void(*_free)(void*)){ char modFile[MAX_PATH]; int i=strlen(file)-1; FILE *out=NULL; if(!oldPtr && !newPtr) return NULL; while(out==NULL) out=fopen("\\debug_alloc.txt", "ab"); strcpy(modFile, file); while(i>=0){ if(modFile[i]==':') modFile[i] = '_'; i--; } if(out){ fprintf(out, "%d:%s:%s:%d:%d:%.8x:%.8x:%d:%d:\r\n", (int)time(NULL), modFile, func, line, getpid(), (size_t)oldPtr, (size_t)newPtr, oldSize, newSize); fclose(out); }else{ fprintf(stderr, "%d:%s:%s:%d:%d:%.8x:%.8x:%d:%d:\r\n", (int)time(NULL), modFile, func, line, getpid(), (size_t)oldPtr, (size_t)newPtr, oldSize, newSize); } if(_free && oldPtr) _free(oldPtr); return newPtr; }
-
I use macros as a wrapper to the malloc, realloc and free functions. This way I can log all my memory allocations to a file, the following is a bit complicated, but works: #define realloc(PTR, SIZE) \ logReallocPtr(PTR, realloc(PTR, SIZE), ck_memsize(PTR), SIZE, __FILE__, #PTR, __LINE__, NULL) #define malloc(SIZE) \ logReallocPtr(NULL, malloc(SIZE), 0, SIZE, __FILE__, #SIZE, __LINE__, NULL) #define free(PTR) \ logReallocPtr(PTR, NULL, ck_memsize(PTR), 0, __FILE__, #PTR, __LINE__, free) // This function gets called for every malloc, realloc and free void *logReallocPtr(void *oldPtr, void *newPtr, size_t oldSize, size_t newSize, const char *file, const char *func, size_t line, void(*_free)(void*)){ char modFile[MAX_PATH]; int i=strlen(file)-1; FILE *out=NULL; if(!oldPtr && !newPtr) return NULL; while(out==NULL) out=fopen("\\debug_alloc.txt", "ab"); strcpy(modFile, file); while(i>=0){ if(modFile[i]==':') modFile[i] = '_'; i--; } if(out){ fprintf(out, "%d:%s:%s:%d:%d:%.8x:%.8x:%d:%d:\r\n", (int)time(NULL), modFile, func, line, getpid(), (size_t)oldPtr, (size_t)newPtr, oldSize, newSize); fclose(out); }else{ fprintf(stderr, "%d:%s:%s:%d:%d:%.8x:%.8x:%d:%d:\r\n", (int)time(NULL), modFile, func, line, getpid(), (size_t)oldPtr, (size_t)newPtr, oldSize, newSize); } if(_free && oldPtr) _free(oldPtr); return newPtr; }