Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory leak detection... the best solution?

Memory leak detection... the best solution?

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancequestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raphael Kindt
    wrote on last edited by
    #1

    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

    N H 2 Replies Last reply
    0
    • R Raphael Kindt

      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

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • R Raphael Kindt

        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

        H Offline
        H Offline
        Hari Krishnan Noida
        wrote on last edited by
        #3

        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~

        R 1 Reply Last reply
        0
        • H Hari Krishnan Noida

          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~

          R Offline
          R Offline
          Raphael Kindt
          wrote on last edited by
          #4

          And without MFC...??? Hello World!!! :) from Raphaël

          H P 2 Replies Last reply
          0
          • R Raphael Kindt

            And without MFC...??? Hello World!!! :) from Raphaël

            H Offline
            H Offline
            Hari Krishnan Noida
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • R Raphael Kindt

              And without MFC...??? Hello World!!! :) from Raphaël

              P Offline
              P Offline
              paul4
              wrote on last edited by
              #6

              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; }

              P 1 Reply Last reply
              0
              • P paul4

                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; }

                P Offline
                P Offline
                paul4
                wrote on last edited by
                #7

                Oh, and ck_memsize(PTR) should be replaced with _msize(PTR).

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups