Thread: How to detect a memory leak?

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    How to detect a memory leak?

    Hi, I have other questions. Because these questions don't have anything to do with STL, I might as well post them into a different thread.

    My code is turning into a small beast now. I know that I have memory leak somewhere along the code, but because it's bigger than what it was before, I can't trace all the 'new' and 'delete' in it. The question is: How do I detect memory leaks?

    BTW, another memory related question. Why doesn't this code work?

    Code:
    char *foo;
    char foo2[12]="Hello world!";
    
    foo=new char[strlen(foo2)];
    strcpy(foo,foo2);
    delete[] foo;

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    First: What is the [] in front of delete?
    Second: strlen("Hello world!") == 13 not 12. You should give it an additional byte for '\0' == NULL that indicates the end of the string in the memory. Its better to write
    Code:
    char *foo;
    char foo2[]="Hello world!";
    
    foo=new char[strlen(foo2)];
    strcpy(foo,foo2);
    delete foo;
    Last edited by siavoshkc; 07-19-2006 at 02:02 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The [] in front of delete tells the delete operator that it needs to delete the entire array pointer to by the pointer in the brackets. If you fail to use [] for delete on arrays, it will only delete the first element and not the entire array. So if you have an array of 100 class objects and you call delete without using [] for that array, you will have 99 objects whose constructor is never
    called and so are never cleaned up.

    Code:
    int *pArray=new int[100];
    for (int i=0;i<100;i++)
    {
      pArray[i]=i;
    }
    
    delete pArray;    //Incorrect
    delete [] pArray; //Correct

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Well, acutally, the string was "char[255]" (I used it to read from a file using fgets and since the string's length can't be determined, I used 255). And the problem still occur.

    Quote Originally Posted by siavoshkc
    First: What is the [] in front of delete?
    Second: strlen("Hello world!") == 13 not 12. You should give it an additional byte for '\0' == NULL that indicates the end of the string in the memory. Its better to write
    Code:
    char *foo;
    char foo2[]="Hello world!";
    
    foo=new char[strlen(foo2)];
    strcpy(foo,foo2);
    delete foo;

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Bubba
    The [] in front of delete tells the delete operator that it needs to delete the entire array pointer to by the pointer in the brackets. If you fail to use [] for delete on arrays, it will only delete the first element and not the entire array.
    This is actually incorrect. If operator new [] is used to create an array, operator delete [] must be used to release it. Failure to do so yields undefined behaviour according to the standard. In practice, your statement "it will only delete the first element and not the entire array" occurs with some compilers/libraries, but not all.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Sorry I made a big mistake strlen("Hello world!") == 12 so you should write
    Code:
    char *foo;
    char foo2[]="Hello world!";
    
    foo=new char[strlen(foo2)+1]; //You give foo one additional byte for NULL
    strcpy(foo,foo2);
    delete[] foo;
    This code worked fine in my VC++.

    And about []:
    1) <<It was stupid question.
    2) How delete operator determines the size of allocated memory pointed by foo?
    Last edited by siavoshkc; 07-19-2006 at 05:14 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by siavoshkc
    1) If we use [] it will delete both allocated memory and pointer or just allocated memory?
    2) How delete operator determines the size of allocated memory pointed by foo?
    It deletes the allocated memory. You can't delete the pointer as long as it resides on the stack. Not conventionally, anyway. After you call delete[] you should still null the pointer you freed, just like a regular call to delete.

    I'm not sure how it determines the size in memory. I'd guess it probably maintained the value of the constant used in the new[] operator.
    Sent from my iPadŽ

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Memory management is implementation-specific. Typically, the memory manager will store a bit of metadata in front of the allocated block, containing at least the size of the block.

    If you fail to use [] for delete on arrays, it will only delete the first element and not the entire array.
    Typically true regarding destructors, but the memory will usually be freed completely. Not that it matters, since it's undefined.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    So the general consensus is....it's a bad idea either way you go.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Memory management is implementation-specific.
    Its routines should be in msvcrt.dll for Windows , aren't they?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The operator new implementation wil probably be in msvcpxx.dll, where xx is the VC++ version number. Or the debug version of the DLL. Or the static library version, and then compiled into the executable itself.
    MS's operator new does, I think, call malloc() in turn, so that you would find in msvcrt.dll, or whatever C runtime DLL/static lib you're using.
    Of course, malloc() itself is implemented in terms of HeapAlloc, which is in kernel32.dll - but only in Win9x. In the WinNT-series, it is just an entry in kernel32.dll's export table that actually just forwards to RtlAllocHeap in ntdll.dll.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How do I detect memory leaks?
    Depending on your IDE or compiler, there are tools available. I don't remember exactly how to set it up, but try searching for _CrtDumpMemoryLeaks (I think this thread might have some information: http://www.codeguru.com/forum/showthread.php?t=312742).

    >> char foo2[12]="Hello world!";
    While char foo2[]="Hello world!"; works fine, you should know that if you do put in a number, it should be char foo2[13]="Hello world!";

    However, you should be using the C++ string class in C++. That is your best bet for removing memory leaks with character strings. Same thing with vector and dynamic arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Replies: 1
    Last Post: 12-01-2007, 02:06 AM
  4. Is this a memory leak?
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 07-20-2005, 01:15 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM