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. How to detect memory leak in VC++

How to detect memory leak in VC++

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiocsharpperformancetutorial
9 Posts 9 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.
  • Y Offline
    Y Offline
    yellowine
    wrote on last edited by
    #1

    I am using VC (MFC also) to develop programs under Visual Studio as IDE. Could anybody here exlain how to used Visual studio to detect memory leak?

    C J B A P 5 Replies Last reply
    0
    • Y yellowine

      I am using VC (MFC also) to develop programs under Visual Studio as IDE. Could anybody here exlain how to used Visual studio to detect memory leak?

      C Offline
      C Offline
      CreekDawg
      wrote on last edited by
      #2

      Here is an article I found from MSDN. Hope it helps. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/\_core\_detecting\_a\_memory\_leak.asp;)

      1 Reply Last reply
      0
      • Y yellowine

        I am using VC (MFC also) to develop programs under Visual Studio as IDE. Could anybody here exlain how to used Visual studio to detect memory leak?

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        In debug mode, you can use [_CrtMemDifference](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt__crtmemdifference.asp) and related functions (in <crtdbg.h>) to check for memory leaks caused by C run-time library allocations (malloc, realloc and new). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        1 Reply Last reply
        0
        • Y yellowine

          I am using VC (MFC also) to develop programs under Visual Studio as IDE. Could anybody here exlain how to used Visual studio to detect memory leak?

          B Offline
          B Offline
          Bill Wilson
          wrote on last edited by
          #4

          GOOD LUCK! C Runtime library for addressing memory, look up _CRT in the MSDN index. Unfortunately, they don't account for all the memory. Beyond that, BoundsChecker by Numega is probably the most popular tool for this purpose. Hope this helps Bill

          N 1 Reply Last reply
          0
          • Y yellowine

            I am using VC (MFC also) to develop programs under Visual Studio as IDE. Could anybody here exlain how to used Visual studio to detect memory leak?

            A Offline
            A Offline
            Atul Dharne
            wrote on last edited by
            #5

            Check the CMemoryState class which can be used for debug versions only. Atul Sonork 100.13714 netdiva

            1 Reply Last reply
            0
            • Y yellowine

              I am using VC (MFC also) to develop programs under Visual Studio as IDE. Could anybody here exlain how to used Visual studio to detect memory leak?

              P Offline
              P Offline
              Paul M Watt
              wrote on last edited by
              #6

              first make this declaration at the top of your stdafx.h file: #define _CRTDBG_MAP_ALLOC next make sure that these files are included in your project: #include #include finally, when your program shutsdown, you need to call this function: _CrtDumpMemoryLeaks(); This will produce an output like this: Detected memory leaks! Dumping objects -> C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18} normal block at 0x00780E80, 64 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. You can click on filename with the line number and it will take you to the place where you allocated the memory. This will be a good place to start. After you get a list of objects that are leaking, you will now have the allocation number of the block that was leaked. In the example above (18) was the block. You can run the program again and set a break point when block 18 is allocated. First set a break point right when your program loads. Declare a variable in your watch window called: _crtBreakAlloc Set this variable to 18, this will tell the debugger to break when the 18 block is allocated. With this you can step through the code and know exactly which element that you allocated that is leaking. With this technique it is very important that you perform the exact same operations in the program in order to make the memory allocation follow the same path. Look up this article on MSDN for more information about this technique: Detecting and Isolating Memory Leaks Using Microsoft Visual C++ by Edward Wright This technique is a good, free, place to start, but like other people have said, BoundsChecker is a good place to go if you have the money to buy this tool.

              R 1 Reply Last reply
              0
              • P Paul M Watt

                first make this declaration at the top of your stdafx.h file: #define _CRTDBG_MAP_ALLOC next make sure that these files are included in your project: #include #include finally, when your program shutsdown, you need to call this function: _CrtDumpMemoryLeaks(); This will produce an output like this: Detected memory leaks! Dumping objects -> C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18} normal block at 0x00780E80, 64 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. You can click on filename with the line number and it will take you to the place where you allocated the memory. This will be a good place to start. After you get a list of objects that are leaking, you will now have the allocation number of the block that was leaked. In the example above (18) was the block. You can run the program again and set a break point when block 18 is allocated. First set a break point right when your program loads. Declare a variable in your watch window called: _crtBreakAlloc Set this variable to 18, this will tell the debugger to break when the 18 block is allocated. With this you can step through the code and know exactly which element that you allocated that is leaking. With this technique it is very important that you perform the exact same operations in the program in order to make the memory allocation follow the same path. Look up this article on MSDN for more information about this technique: Detecting and Isolating Memory Leaks Using Microsoft Visual C++ by Edward Wright This technique is a good, free, place to start, but like other people have said, BoundsChecker is a good place to go if you have the money to buy this tool.

                R Offline
                R Offline
                Rick York
                wrote on last edited by
                #7

                What I do is I also use the define #define _CRTDBG_MAP_ALLOC My stdafx.h has this in it : #ifdef _CRTDBG_MAP_ALLOC #include "crtdbg.h" #endif and I call this little function in OnInitInstance or during initialization for non-MFC apps. void EnableMemoryLeakDump( void ) { #ifdef _DEBUG int tmp = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); tmp |= _CRTDBG_LEAK_CHECK_DF; tmp &= ~(_CRTDBG_CHECK_CRT_DF ); _CrtSetDbgFlag( tmp ); #endif // _DEBUG } I don't call anything else and I get leak reports on termination. This works for MFC and straight-Win32 apps.

                1 Reply Last reply
                0
                • B Bill Wilson

                  GOOD LUCK! C Runtime library for addressing memory, look up _CRT in the MSDN index. Unfortunately, they don't account for all the memory. Beyond that, BoundsChecker by Numega is probably the most popular tool for this purpose. Hope this helps Bill

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #8

                  Bill Wilson wrote: Beyond that, BoundsChecker by Numega is probably the most popular tool for this purpose. Freeware??? Nish CPUA # 0x0666 Sonork ID 100.9786 voidmain www.busterboy.org

                  S 1 Reply Last reply
                  0
                  • N Nish Nishant

                    Bill Wilson wrote: Beyond that, BoundsChecker by Numega is probably the most popular tool for this purpose. Freeware??? Nish CPUA # 0x0666 Sonork ID 100.9786 voidmain www.busterboy.org

                    S Offline
                    S Offline
                    Shog9 0
                    wrote on last edited by
                    #9

                    Nish [BusterBoy] wrote: Freeware??? I wish... something like $300 per license. farewell goodnight last one out turn out the lights

                    Smashing Pumpkins, Tales of a Scorched Earth

                    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