locked
Memory leak? RRS feed

  • Question

  • This is a memory leak right?

    {
      char* b = new char[9];		
      std::auto_ptr<char> buffer(b);
    }

    • Moved by Damon Zheng Wednesday, December 19, 2012 11:21 AM forum retired (From:Visual C++ Language)
    Friday, November 30, 2012 9:41 AM

Answers

  • According to one of my colleagues this is not a leak in Visual Studio (VC9) but according to the standard this is a leak. Can someone verify that this is not a leak in VC9?

    That certainly appears to be the case.

    #include "stdafx.h"
    
    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    #include <memory>
    
    #ifdef _DEBUG
    #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_CLIENTBLOCK
    #endif
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    {
      char* b = new char[9];
      std::auto_ptr<char> buffer(b);
    }
      _CrtDumpMemoryLeaks();
      return 0;
    }
    


    Output:

    'mem leak detect.exe': Loaded 'C:\Documents and Settings\...\My Documents\Visual Studio 2008\Projects\mem leak detect\Debug\mem leak detect.exe', Symbols loaded.
    'mem leak detect.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
    'mem leak detect.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
    'mem leak detect.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Symbols loaded.
    The program '[2424] mem leak detect.exe: Native' has exited with code 0 (0x0).
    
    

    Without the auto_ptr:

    {
      char* b = new char[9];		
    //  std::auto_ptr<char> buffer(b);
    }
    

    Output:

    'mem leak detect.exe': Loaded 'C:\Documents and Settings\...\My Documents\Visual Studio 2008\Projects\mem leak detect\Debug\mem leak detect.exe', Symbols loaded.
    'mem leak detect.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
    'mem leak detect.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
    'mem leak detect.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Symbols loaded.
    Detected memory leaks!
    Dumping objects ->
    c:\documents and settings\...\my documents\visual studio 2008\projects\mem leak detect\mem leak detect\mem leak detect.cpp(27) : {92} client block at 0x00349360, subtype 0, 9 bytes long.
     Data: <         > CD CD CD CD CD CD CD CD CD 
    Object dump complete.
    The program '[2756] mem leak detect.exe: Native' has exited with code 0 (0x0).
    

    Used VC++ 2008 Express (no SP1).

    - Wayne

    • Marked as answer by Anonymous7520 Friday, November 30, 2012 4:05 PM
    Friday, November 30, 2012 1:39 PM
  • Anonymous7520 wrote:

    This is a memory leak right?

    {
     char* b = new char[9];
     std::auto_ptr<char> buffer(b);
    }

    This code exhibits undefined behavior. Memory allocated with new[] must be deallocated with delete[], as in

    delete[] b;

    But std::auto_ptr doesn't know that, and would effectively execute  "delete b;" in its destructor, which triggers undefined behavior.

    It so happens that in MSVC, "delete p" and "delete[] p" are equivalent  for a pointer p of type T* as long as T is a type with a trivial  destructor (which includes all fundamental types, char among them). So  your code will work with MSVC with no apparent ill effects. But, again,  it's technically illegal, and thus non-portable.


    Igor Tandetnik

    • Marked as answer by Anonymous7520 Friday, November 30, 2012 4:05 PM
    Friday, November 30, 2012 2:50 PM

All replies

  • Hi,

    Please look into the similar thread in the following link.

    http://stackoverflow.com/questions/261336/is-it-wrong-to-use-auto-ptr-with-new-charn


    Gopinath.S

    Friday, November 30, 2012 10:15 AM
  • To avoid any suspicion, use this definition:

    std::unique_ptr<char[]> buffer(b);

    Friday, November 30, 2012 10:35 AM
  • thats not my question, I was asking about the exact code I wrote.
    Friday, November 30, 2012 11:45 AM
  • According to one of my colleagues this is not a leak in Visual Studio (VC9) but according to the standard this is a leak. Can someone verify that this is not a leak in VC9?
    Friday, November 30, 2012 12:41 PM
  • According to one of my colleagues this is not a leak in Visual Studio (VC9) but according to the standard this is a leak. Can someone verify that this is not a leak in VC9?

    That certainly appears to be the case.

    #include "stdafx.h"
    
    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    #include <memory>
    
    #ifdef _DEBUG
    #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_CLIENTBLOCK
    #endif
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    {
      char* b = new char[9];
      std::auto_ptr<char> buffer(b);
    }
      _CrtDumpMemoryLeaks();
      return 0;
    }
    


    Output:

    'mem leak detect.exe': Loaded 'C:\Documents and Settings\...\My Documents\Visual Studio 2008\Projects\mem leak detect\Debug\mem leak detect.exe', Symbols loaded.
    'mem leak detect.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
    'mem leak detect.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
    'mem leak detect.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Symbols loaded.
    The program '[2424] mem leak detect.exe: Native' has exited with code 0 (0x0).
    
    

    Without the auto_ptr:

    {
      char* b = new char[9];		
    //  std::auto_ptr<char> buffer(b);
    }
    

    Output:

    'mem leak detect.exe': Loaded 'C:\Documents and Settings\...\My Documents\Visual Studio 2008\Projects\mem leak detect\Debug\mem leak detect.exe', Symbols loaded.
    'mem leak detect.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
    'mem leak detect.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
    'mem leak detect.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Symbols loaded.
    Detected memory leaks!
    Dumping objects ->
    c:\documents and settings\...\my documents\visual studio 2008\projects\mem leak detect\mem leak detect\mem leak detect.cpp(27) : {92} client block at 0x00349360, subtype 0, 9 bytes long.
     Data: <         > CD CD CD CD CD CD CD CD CD 
    Object dump complete.
    The program '[2756] mem leak detect.exe: Native' has exited with code 0 (0x0).
    

    Used VC++ 2008 Express (no SP1).

    - Wayne

    • Marked as answer by Anonymous7520 Friday, November 30, 2012 4:05 PM
    Friday, November 30, 2012 1:39 PM
  • Anonymous7520 wrote:

    This is a memory leak right?

    {
     char* b = new char[9];
     std::auto_ptr<char> buffer(b);
    }

    This code exhibits undefined behavior. Memory allocated with new[] must be deallocated with delete[], as in

    delete[] b;

    But std::auto_ptr doesn't know that, and would effectively execute  "delete b;" in its destructor, which triggers undefined behavior.

    It so happens that in MSVC, "delete p" and "delete[] p" are equivalent  for a pointer p of type T* as long as T is a type with a trivial  destructor (which includes all fundamental types, char among them). So  your code will work with MSVC with no apparent ill effects. But, again,  it's technically illegal, and thus non-portable.


    Igor Tandetnik

    • Marked as answer by Anonymous7520 Friday, November 30, 2012 4:05 PM
    Friday, November 30, 2012 2:50 PM