Detecting Memory leak in Visual Studio (Debug mode)

This is a discussion on Detecting Memory leak in Visual Studio (Debug mode) within the Windows Programming forums, part of the Platform Specific Boards category; The memory leak information is printed on "Output" window. [ This advantage is in "Debug" mode only. ] Example Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    Detecting Memory leak in Visual Studio (Debug mode)

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    What's your point?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 11:03 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11: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, 02:41 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21