How to detect memory leaks in MFC?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: How to detect memory leaks in MFC?

  1. #1
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,027

    How to detect memory leaks in MFC?

    Q: Which is the simplest method for detecting memory leaks in MFC code?


    A: Detecting memory leaks in MFC code is pretty simple because the MFC framework has bult-in support for that purpose.
    Let’s take a short example:
    Code:
    #include "StdAfx.h"
    #include "Foo.h"
    
    CFoo::CFoo()
    {
       m_buffer = new char[128];
       strcpy(m_buffer, "Baba Safta");
    }
    
    CFoo::~CFoo()
    {
    }
    Also presume that delete operator is never called for freeing m_buffer.
    After running then closing the program in debug mode, we can see in Debug window:
    Code:
    Detected memory leaks!
    Dumping objects ->
    {183} normal block at 0x0036B320, 128 bytes long.
     Data:  42 61 62 61 20 53 61 66 74 61 00 CD CD CD CD CD 
    Object dump complete.
    The program '[1758] MyTest.exe: Native' has exited with code 0 (0x0).
    It shows that we have memory leaks and some additional info but it doesn't tell us the cause.
    To find out the source code which generates memory leaks, we simply have to use DEBUG_NEW MFC macro.
    Code:
    #include "StdAfx.h"
    #include "Foo.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    CFoo::CFoo()
    {
       m_buffer = new char[128]; // <-- double click in "Debug" window to go here!
       strcpy(m_buffer, "Baba Safta");
    }
    
    CFoo::~CFoo()
    {
       // <-- should free the buffer to avoid memory leaks!
    }
    Now, the Debug window looks a little bit different:
    Code:
    Detected memory leaks!
    Dumping objects ->
    c:\Projects\Test\Foo.cpp(10) : {183} normal block at 0x0036B320, 128 bytes long.
     Data:  42 61 62 61 20 53 61 66 74 61 00 CD CD CD CD CD 
    Object dump complete.
    The program '[3950] Test.exe: Native' has exited with code 0 (0x0).
    Additional info were been added: the source file (c:\Projects\Test\Foo.cpp) and the line number which gnerates memory leaks (10). Moreover, if double-click in Debug window, the Visual Studio IDE jumps exactly in that source file at that line number.

    Resources:


    See also:
    Last edited by ovidiucucu; December 26th, 2012 at 03:48 AM.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center