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

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Simply put, your copy constructor is flawed.
    If you have pointers inside the class and you copy those pointers to the new class, both pointers will be invalid when the destructor is run. This is called shallow copying.
    What you do is allocate new memory in the new class and copy over the data inside the pointer from your class to the new. That way, each destructor can release its own data, no more error.
    Or better yet, just use a smart pointer! Then the object won't be deleted so long as it's in use.
    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.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    Or better yet, just use a smart pointer! Then the object won't be deleted so long as it's in use.
    Aren't there different types of smart pointers than reference counting pointers? Like auto_ptr?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, there are. A reference counting one, such as shared_ptr (tr1/boost) will do the job fine.
    Or you could reinvent the wheel and make your own one.
    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.

  4. #19
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Elysia View Post
    Simply put, your copy constructor is flawed.
    If you have pointers inside the class and you copy those pointers to the new class, both pointers will be invalid when the destructor is run. This is called shallow copying.
    What you do is allocate new memory in the new class and copy over the data inside the pointer from your class to the new. That way, each destructor can release its own data, no more error.
    Or better yet, just use a smart pointer! Then the object won't be deleted so long as it's in use.
    I understand what you're saying but I can't quite do that. The object in question IS a pointer but in the destructor the problem isn't deleteing the pointer or not having copied the pointer's data in the copy constructor, instead, in the destructor I have to call:

    Code:
    env->DeleteLocalRef( theData );
    DeleteLocalRef releases the memory within the Java VM. I have to do this when I'm done with "theData" which normally is in the destructor. However, if the containing class is copied, I don't want to call that until ALL copies are done. Is there a way to do this?

    Is it bad design/practice to do it with a static variable for the class that's increased with each call of the copy constructor, and then decreased when the destructor's called 9and then at zero it releases it)? Is there a better way?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is a better way. Reference counting. Call the function when the reference reaches 0.
    I think there's a smart pointer (at least in boost) that can call a custom deleter function when the object is about to be deleted.
    Maybe look into that. Otherwise you can make your own smart pointer to do the work.
    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.

  6. #21
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Elysia View Post
    There is a better way. Reference counting. Call the function when the reference reaches 0.
    I think there's a smart pointer (at least in boost) that can call a custom deleter function when the object is about to be deleted.
    Maybe look into that. Otherwise you can make your own smart pointer to do the work.
    Thanks.

    Yeah auto_ptr won't do it but I've now read references to smart pointers that count down the # of ref's and delete only when it hits 0. But I can't seem to find a mention of the name of that smart pointer implementation. Anyone know of one (so I don't have to reinvent the wheel)?

    Thanks!

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There's one called shared_ptr. It's available in boost and TR1 implementations.
    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. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by 6tr6tr View Post
    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?
    No. The idea is to lessen the burden of memory management, not increase it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #24
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Just out of curiosity, do you have any learning material you are using, most good reference materials address a lot of the stuff you're posting on the boards. Don't take the shotgun approach.

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by indigo0086 View Post
    Don't take the shotgun approach.
    There's a lot of that going around. You don't necessarily have to read a C++ book from cover to cover, but at least have one for a reference. A number of "best practices" books should be in your library, as well.

  11. #26
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    A copy constructor should only be creating a new object from the data members of another, not deallocating anything. You don't need seperate methods to release memory because that's what the delete keyword is for.

    in the ctor you'd do something like

    Code:
    SomeClass(const SomeClass& other)
    {
       member1 = other.member1;
       member2 = other.member2;
       //...etc, etc
    }
    There is no returning of a value. More importantly a copy constructor should properly handle the copying of member pointers. So if other has a pointer to some other object, then the SomeClass copy constructor should construct a new object with that data as well, not just point to the object, because during clean-up, you will delete one reference while the other class is left with a pointer to garbage.

    Code:
    SomeClass(const SomeClass& other)
    {
       //allocating a pointer member
       p_OtherClass = new Otherclass(*(other.p_OtherClass));
       //.....
    }
    Last edited by indigo0086; 04-29-2008 at 06:36 AM.

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