CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    2

    Detecting memory leak in complicating source code

    Hello everyone,

    I'am trying to run a code which performs continuously min-cuts to a graph until it detects dense subgraphs.
    Unfortunately, it seems that there is a memory leak and the program exits. As the program is constituted of many files, it is really difficult to detect the memory leak error.
    I was wondering if there is a way to print the size of the allocated memory after each min-cut in order to test my code and find the memory leak.

    Thank you!!

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Detecting memory leak in complicating source code

    Normally the Visual Studio tells you (in debug mode) that you are leaking memory, and when you are using new/delete it will even tell you where you are leaking it.

  3. #3
    Join Date
    Aug 2004
    Location
    Chennai, India.
    Posts
    380

    Lightbulb Re: Detecting memory leak in complicating source code

    Hi,

    You can make use of CMemoryState class

    Code:
    CMemoryState state1, state2, memDiff;
    
    state1.Checkpoint();
    
    ....
    ....
    ....
    
    state2.Checkpoint();
    
    if ( memDiff.Difference ( state1, state2 ) )
    {
    //..LEAKED!!!
    }
    Hope it helps..

    Thanks,
    dwurity

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

    Re: Detecting memory leak in complicating source code

    The absolute easiest way to locate memory leaks is to use a tool like valgrind (Linux). I don't know of a Windows equivalent which is quite as easy, at least not a free one. The above will work if you're willing to make source code changes.

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Detecting memory leak in complicating source code

    Quote Originally Posted by Lindley View Post
    The absolute easiest way to locate memory leaks is to use a tool like valgrind (Linux). I don't know of a Windows equivalent which is quite as easy, at least not a free one. The above will work if you're willing to make source code changes.
    wake up and check where you posted this comment in :
    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

  6. #6
    Join Date
    Mar 2009
    Posts
    2

    Re: Detecting memory leak in complicating source code

    Thank you guys for your responce!

    @dwurity,
    how can I use CMemoryState? I included in my code but i receive a code error. Should i include any library or something like this?

  7. #7
    Join Date
    Jan 2010
    Posts
    24

    Re: Detecting memory leak in complicating source code

    The memory leak information is printed on "Output" window.
    [ This advantage is in "Debug" mode only. ]
    Example

    int _tmain()
    {
    int* ptr = NULL;

    ptr = (int*)malloc(sizeof(int)*20);

    for( int i=0; i<20; ++i )
    ptr[i] = i;

    #ifndef NDEBUG

    int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag

    flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit

    _CrtSetDbgFlag(flag); // Set flag to the new value

    #endif

    //free(ptr);

    printf("\n\nPress Enter...");
    getch();
    return 0;
    }


    http://www.mindfiresolutions.com/Det...g-mode-749.php

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
  •  





Click Here to Expand Forum to Full Width

Featured