CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Detecting memory leaks (VC8)

    When I was using VC6 I could run a program in the debugger and, on closing the program down, VC6's debug output window would flag up a message if the program had leaked any memory. I don't seem to see this any more with VC8. Is there a setting somewhere that I need to enable?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Detecting memory leaks (VC8)

    I don't know about any option for that, but if exists it is definitely enabled by default. I never had to check anything for that and it works in all the versions I'm using (2005, 2008, 2010). Maybe you just don't have memory leaks.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: Detecting memory leaks (VC8)

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <conio.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int *p = new int[20];
    	printf("Press ENTER to quit");
    	getchar();
    
    	return 0;
    }
    Hmmm.... am I doing something silly? The above console app should flag up a memory leak in the output window after completion, right?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Detecting memory leaks (VC8)

    Quote Originally Posted by John E View Post
    right?
    Nope.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Detecting memory leaks (VC8)

    The memory leaks were reported when you use the macro DEBUG_NEW in your sources which actually provides a different operator new and operator delete where all allocations and deallocations were watched.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Detecting memory leaks (VC8)

    Basically, the memory leaks are reported in debug mode when call _CrtDumpMemoryLeaks.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: Detecting memory leaks (VC8)

    Interesting.... I've never #included crtdbg.h in my life but this always worked before. Having said that, my old VC6 programs invariably used MFC which I haven't been using since moving to VC8. Maybe that's how crtdbg.h was getting included. Thanks all, for the info.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  9. #9
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Detecting memory leaks (VC8)

    MFC framework and MFC wizards take care of many things which usually we don't even think about...
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  10. #10
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: Detecting memory leaks (VC8)

    Just found some time to try this and yes, basically it works. According to that article I should see some output looking 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.
    and indeed I do see such output as long as I allocated my memory using malloc() - but in my original example (using new) I see this:-

    Detected memory leaks!
    Dumping objects ->
    e:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1150) : {90} normal block at 0x00351A60, 80 bytes long.
    Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
    Object dump complete.
    Obviously, that's showing me the location of the debug new operator in crtdbg.h, rather than the position in my source file where new got used. Is there a way to fix this?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  11. #11
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Detecting memory leaks (VC8)

    Just use crtBreakAlloc.

    Viggy

  12. #12
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Detecting memory leaks (VC8)

    If you use
    Code:
    #ifdef _DEBUG
       #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
    #else
       #define DEBUG_CLIENTBLOCK
    #endif
    in your 'general header include' and
    Code:
    #ifdef _DEBUG
      #define new DEBUG_CLIENTBLOCK
    #endif
    in all your ccp-files

    In the code I use this I also do
    Code:
    #if defined(_DEBUG)
      int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
      tmpFlag    |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF;  // Check heap alloc and dump mem leaks at exit
      _CrtSetDbgFlag( tmpFlag );
      //_crtBreakAlloc = 58652; // in case something very bad happens
      assert( !errno );
    #endif
    as the very first thing in main
    Last edited by S_M_A; January 15th, 2011 at 09:29 AM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  13. #13
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: Detecting memory leaks (VC8)

    Thanks guys, I'll try your suggestions later today. MFC has obviously been handling this "under the hood" for many years without me realising the intricacies. The particular project that I'll be building will be using GTK+ though, rather than MFC and I can already see that detecting memory leaks will be considerably more complicated than I thought. Here's a modified version of the simple console app that I used before:-

    Code:
    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    #include "stdafx.h"
    #include <stdio.h>
    #include <conio.h>
    #include <string>
    #include "..\..\..\+GTK-SOURCES\gnu-mm\libs\glibmm2\glibmm\ustring.h"
    
    const std::string    strTest1 = "A test string";
    const Glib::ustring  strTest2 = "Another test string";
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int *p = (int*)malloc(20 * sizeof(int));
    
    	printf("Press ENTER to quit");
    	getchar();
    
    	_CrtDumpMemoryLeaks();
    	return 0;
    }
    I haven't yet added those new features you guys mentioned but note the global objects strTest1 and strTest2. _CrtDumpMemoryLeaks() flags up a memory leak from strTest2 (a Glib::ustring) presumably because global objects haven't yet released their memory by the time I called _CrtDumpMemoryLeaks().

    What's interesting is that no leak got detected from strTest1 (which was a std::string). Things get really interesting though if I build a simple MFC app instead of my console app. In that case, the leak from malloc() gets (correctly) detected but no leaks get detected from either of the global objects.

    Clearly MFC is pretty clever about where exactly it calls _CrtDumpMemoryLeaks() and I guess I've a lot more to learn about using this feature properly. Maybe this is a good reason to go back to MFC....

    No, only joking. This is a cross-platform app, so MFC is out of the question.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  14. #14
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Detecting memory leaks (VC8)

    We, the MFC fans, were all the years looking for someone who makes MFC a cross-platform library

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Detecting memory leaks (VC8)

    Personally I find it rather difficult to determine exactly which gtk+ objects need explicit deletion and which don't, since gtk+ uses some kind of reference counting which isn't documented all that well.

    This might be one good reason to choose gtkmm instead; C++ libraries typically do memory stuff a bit more automatically. I don't know gtkmm well enough to say how much they've cleaned things up though.

Page 1 of 3 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured