Thread: Detecting Memory leak in Visual Studio (Debug mode)

  1. #1
    Registered User
    Join Date
    Jan 2010

    Detecting Memory leak in Visual Studio (Debug mode)

    The memory leak information is printed on "Output" window.
    [ This advantage is in "Debug" mode only. ]
    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
    Last edited by Salem; 05-06-2010 at 11:11 AM. Reason: Added code tags, removed spam links

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    What's your point?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    This is not even a complete example. Where is the rest of the main function? What do you use flag for? Can you explain what the rows under #ifndef NDEBUG do? If NDEBUG is defined, do you initialize flag to something else?
    Come on, you can do it! b( ~_')

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    NDEBUG is one of the very few standard macros for conditional compilation.

    Typically, it controls the assert() test, enabling it in debug mode, and removing it in release mode.
    C Guide--2.1 assert.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

Tags for this Thread