Thread: If vector holds objects, will it delete the pointers when destroyed?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    If vector holds objects, will it delete the pointers when destroyed?

    If I have a vector of SomeClass objects and those objects are created inside another method using "new", will the vector properly handle memory management when destroyed? Or do i have to keep track of them and delete them myself?

    Code:
    //I have no control over the type of this, 
    //so I have to populate a vector<SomeClass>
    //instead of vector<SomeClass*>
    
    SomeClass getData()
    {
         return * ( new SomeClass() );
    }
    
    int main()
    {
         std::vector<SomeClass> r = getDataHolder();
         r.push_back( getData() );
    }

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    There is a memory leak in your code, you keep no reference nor deallocate the object who's copy you are using to put in a stack
    It's actually pretty redundant and messy. You allocate twice, once on the heap and once on the stack.

    Code:
    r.push_back(SomeClass());

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by indigo0086 View Post
    There is a memory leak in your code, you keep no reference nor deallocate the object who's copy you are using to put in a stack
    It's actually pretty redundant and messy. You allocate twice, once on the heap and once on the stack.

    Code:
    r.push_back(SomeClass());
    OK, the problem is if I do:

    Code:
    SomeClass create()
    {
        return SomeClass()
    }
    then the member data inside SomeClass is not properly maintained and gives weird results. Is the only solution to return a pointer and hold a reference to that somewhere else? (Since I have to work with that vector of objects)?

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    get rid of the create() function and just push it back manually.

    Do you have a proper destructor in the SomeClass() method? A proper copy constructor?

  5. #5
    coder
    Join Date
    Feb 2008
    Posts
    127
    I've made some tests: IMO returning by reference seems to be the best choice.
    Code:
    SomeClass & getData()
    {
         return * new SomeClass();
    }
    
    int main()
    {
         std::vector<SomeClass> r = getDataHolder();
         r.push_back( getData() );
    }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why not to
    r.push_back( SomeClass());

    to avoid memory leak?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That still leaks memory. You shouldn't be getting errors if you do not allocate on the heap. IF you do, then something is wrong in your code...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by carlorfeo View Post
    I've made some tests: IMO returning by reference seems to be the best choice.
    Code:
    SomeClass & getData()
    {
         return * new SomeClass();
    }
    
    int main()
    {
         std::vector<SomeClass> r = getDataHolder();
         r.push_back( getData() );
    }
    This won't have any memory leaks? Vector will properly handle deleting the memory later?

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You have these auxilarry classes that essentually do the same job with a bit more brevity than just using it directly.

    either have a vector of SomeClass pointers and push back new, or have a vector of SomeClass objects and push back the object.

    It leaks memory because you are not returning the address to the newly created SomeClass object.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by 6tr6tr View Post
    This won't have any memory leaks? Vector will properly handle deleting the memory later?
    No it won't.
    All it will do is that vector will make a copy of the object and store (and delete) later. The original object is leaked.
    Everything allocated by new must be deleted.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Elysia View Post
    No it won't.
    All it will do is that vector will make a copy of the object and store (and delete) later. The original object is leaked.
    Everything allocated by new must be deleted.
    Is there a way (through the copy constructor) to return an object from a function without using "new" and have the data all still valid? Or is the only way to create via new (assuming it has to be returned from inside a function)?

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Is there a way (through the copy constructor) to return an object
    Yes - just return an object
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by vart View Post
    Yes - just return an object
    For some reason when I do that, the data inside the object is not properly maintained. Even though I've set the data members in the copy constructor, I'm getting weird results where, when i loop through the vector, it now says those data members are not initialized (e.g. a pointer is empty).

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by 6tr6tr View Post
    Is there a way (through the copy constructor) to return an object from a function without using "new" and have the data all still valid?
    Yes. Write a copy constructor that's not broken.

    It's very simple. If the data is not valid after copying, your copy constructor must be broken, because keeping the data valid is what the cc is for. If there is no possible way to implement the copy constructor correctly, then you're trying to copy an object that is not logically copyable.
    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

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by CornedBee View Post
    Yes. Write a copy constructor that's not broken.

    It's very simple. If the data is not valid after copying, your copy constructor must be broken, because keeping the data valid is what the cc is for. If there is no possible way to implement the copy constructor correctly, then you're trying to copy an object that is not logically copyable.
    Yes, thank you. The problem was the destructor was deleting stuff. So my question then is:

    1. Do I have to create a separate method (like "void destroy()" ) to release all memory instead of using the destructor and then call it explicitly if I'm using the copt ctor?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM
  2. The heap, using new and delete, and pointers
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2001, 09:08 AM
  3. delete array of pointers
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2001, 05:11 AM
  4. delete pointers, but not the pointed object
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2001, 08:04 AM
  5. how to delete objects
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2001, 02:06 AM