tags:

views:

348

answers:

4

How is the destructor for the vector managed when adding elements to this list? Is the object destroyed correctly when it goes out of scope? Are there cases where it would not delete the object correctly? For example what are the consequences if "table" was a child of object, and we added a new table to a vector of object pointers?

vector <object*> _objectList;

_objectList.PushBack(new object);
+7  A: 

Since you're making a vector of "bare" pointers, C++ can't possibly know that the pointers in question are meant to have "ownership" of the objects they point to, and so it will not call those objects' destructors when the pointer goes away. You should use a simple "smart" pointer instead of a "bare" pointer as the vector's item. For example, Boost's shared_ptr would be perfectly adequate for the task (although you can surely do it with "cheaper", lighter-weight approaches, if you don't want to deal with Boost as a whole and have no other need for smart pointers in your code).

Edit: since you (the OP) say that using a framework such as Boost is not feasible, and a couple comments usefully point out that even wrapping std::auto_ptr doesn't really qualify as a decent shortcut, you may have to implement your own smart pointers (or, if you find an open-source, stand-alone smart pointer template class that looks usable, audit it for compliance with your requirements). This article is a useful primer to smart pointers in C++, whether you have to roll your own or audit an existing implementation.

Alex Martelli
I am aware of the boost library, however I am limited to using STL.
auto_ptr does not support copy semantics. So putting it in a vector is definitely not supported and even wrapping it in your own class is going to be tricky.
Martin York
In particular, the copy constructor and operator= of auto_ptr take a non-const reference parameter (which makes sense, since they modify the parameter). So you can just about create a zero-sized vector of them, but there isn't much you can do with it. The functions to add things to vectors all take const parameters, so they don't compile when they try to copy/assign from a const auto_ptr.
Steve Jessop
The article linked above is very good and by a very reputable source "Andrei Alexandrescu". But "Scott Myers" another reputable source has described his own experiences of developing a smart pointer. Even after extensive peer reviewed and publishing; corrections are still coming years after it was published. His basic recommendation is __NOT__ to build your own as it is much more complex then it first appears.
Martin York
@Martin, indeed, if it's feasible to use a tried-and-true existing framework such as Boost, it saves headaches -- but the OP clearly said it's not an option for them. At least they have to support only one use case: a vector of such smart pointers -- so, not all the possible headaches will emerge.
Alex Martelli
With something like shared_ptr, I can't think of many good reasons why it would be possible to write your own, but not possible to use Boost's. Because "your own" would be better off as a copy and paste of Boost's source (which uses only standard C++ AFAIK), than as something you invented. I guess there could be bad reasons: non-compliant compiler; you want to distribute your source but you don't want to credit Boost or cause any confusion who owns what copyrights; someone who doesn't understand the technical issues thinks "Boost" is a 4-letter word, and has banned it out of hand.
Steve Jessop
+2  A: 

In your case, the object pointers are destroyed properly, but the actual objects themselves won't be touched. The STL properly destructs all contained elements - but will not implicitly dereference pointers to types.

Chris
How does one destroy the objects themselves then?
You'll need to iterate over the vector and `delete` the pointers yourself.
ZoogieZork
@ZoogieZork is correct - iterate and delete. The other option (as others have suggested) is to use a smart pointer type.
Chris
+5  A: 

You could use bost 'ptr_vector'. It will automatically destruct objects that the items point to when they are either deleted or the instance of ptr_vector goes out of scope. More info is available here.

Igor Zevaka
I wish I knew about that when I was still doing lots of C++!
Jason D
+1  A: 

STL Vectors make a copy of whatever you put in there, and ultimately delete that copy.

So in this case, the vector is storing a pointer to an object - not the object itself. So it makes a copy of the pointer, and deletes the pointer. But, as Chris said, the object itself will not be deleted.

So, solutions:

If you don't really need to use pointers, then don't:

vector <object> _objectList;
_objectList.PushBack(object());

If you do need to use pointers, you can either use a smart pointer (which handles reference counting for you, and will delete the object along with the pointer), as Alex suggested, or use a ptr_vector, as Igor mentioned.

Smashery
Unfortunately pointers must be used as the base class has some abstract methods