Thread: Is this a memory leak?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Is this a memory leak?

    I need someone with more of a clue than I have to tell me if this code snippet shows a memory leak.


    Code:
    std::list< MyClass > myList;
    
    
    void myFunc ( std::list< MyClass > &myList, int numObjects )
    {
       MyClass * myObj;
    
       for ( int i = 0; i < numObjects; ++i )
       {
          myObj = new MyClass();
          myList.push_back( *myObj ); 
       }
    }

    I have searched online trying to find out if the STL list makes a copy of the object when it's pushed onto the list, which in my mind would definitely cause a leak, or if it will push the actual object.

    I know that I can change the type of data in the list to pointers to avoid this, but I want to know what is going on in this example.

    Many thanks in advance for your help!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, that is a memory leak.

    Yes, it pushes a copy (even if it didn't push a copy, you never delete the pointer anywhere).

    You don't need to use new in your example, so don't and you won't have to worry about memory. If in your actual code, you must use new, then you should store a shared_ptr or a raw pointer in the list. If you use a raw pointer, just remember to delete the object whenever you erase from or clear the list(assuming no other part of your code owns the object as well).

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    It makes a copy to my knowledge. When you push back you are passing a const reference to the object. Which when you do that it will call a copy constructor to copy over the values. So yes it will be a memory leak.
    Woop?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that it is not the const reference passing that makes the copy (since it is a reference), but other code inside the push_back that copies the object.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    this will prevent the memory leak
    Code:
    void myFunc ( std::list< MyClass > &myList, int numObjects )
    {
       for ( int i = 0; i < numObjects; ++i )
       {
    	  MyClass  myObj;
          myList.push_back( myObj ); 
       }
    }

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Thanks for the help. I always seem to do things the round-a-bout hard way ( with errors apparently)!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM