Thread: How to Spot Memory Leaks?

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by matsp View Post
    You must make sure that you delete arrays with the array form of delete:
    Code:
    char *p = new char [10];
    ... 
    
    delete [] p;
    --
    Mats
    Thanks for the info on delete [] everyone!

    I would have definitely flubbed that up haha

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Thanks for the info on delete [] everyone!

    Note that you should almost never be using delete[] in an actual C++ program, because there are few reasons to use a dynamically allocated array. If you think you need new[] and delete[], use vector instead.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by Daved View Post
    Is SCLLRep your class? Does it have an Insert function that takes an Element*? I would look at that as the possible cause.
    Yes it does.

    Main creates a *pointer, and then inserts it into a list.

    Is that copy by value? So delete the pointer after inserting?

    Code:
    string x;
    cin >> x;
    Element* pointerToElement = new Element(x);
    
    Set* pointerToSet = ...
    ...
    pointerToSet->Insert(pointerToElement);
    
    // Delete pointerToElement?
    Last edited by Paul22000; 07-08-2008 at 05:23 PM.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It depends on what the Insert function does. Normally, you don't want to delete the pointer after calling insert, but if the set is storing objects and not pointers, then maybe you do.

    Do you have the code or the documentation on what Insert is doing exactly?

    If the Set owns the objects it holds (i.e. it is storing objects or allocating its own pointers), I wouldn't even be using new in the first place to create the Element, I'd create a local variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for memory leaks
    By Bladactania in forum C Programming
    Replies: 5
    Last Post: 02-10-2009, 12:58 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Tons of memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2005, 10:19 AM
  4. COM Memory Leaks
    By subdene in forum Windows Programming
    Replies: 0
    Last Post: 06-07-2004, 11:57 AM
  5. Memory leaks
    By Malek in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2003, 06:12 PM