Vectors + Classes = Confusing

This is a discussion on Vectors + Classes = Confusing within the C++ Programming forums, part of the General Programming Boards category; Hullo again, I've had a few suggestions recently to move towards vectors instead of just regular old arrays, so I've ...

Vectors + Classes = Confusing

Hullo again, I've had a few suggestions recently to move towards vectors instead of just regular old arrays, so I've started looking into them with more depth. Here's what I've gotten so far:

Code:

//Create a vector with 10 members
std::vector<int> MyInts(10);
//Create a vector with an undefined amount of members
std::vector<int> MyInts();
//And later, say I want to add an element to the vector (I.e. the integer 30)
MyInts.push_back(30)

And that's all pretty decent with me, but my purpose here is to make a vector of classes.

In your destructor for the class just call Vector::clear() which will clean up the memory for the vector.

But it should be cleaned up when it goes out of scope. However I make it a practice to clear the vector before this happens. This is usually in the destructor for the class that makes use of the vector.

Still sort of on the same topic, I'm just wondering, do Vectors have to be cleaned up in some specific way? I thought they did all of that themselves...

Yes, vectors handle all memory management by themselves. There's no need to call any routines when the vector is no longer used.

Bubba, is there any particluar reason that you call clear in the destructor of the class? To me, it just seems to complicate the code for no reason -- one of the advantages of using vector is that you don't have to bother with the destruction.

Thanks for the help guys. I've managed to find the problem, and it was a DirectX problem related to the Vector, but not the actual Vector itself. I may toss the Vector::clear() method into the destructor, just for peace of mind.

In fixing my problem, I had to re-think the use of my vector, and in doing so, I no longer have a vector of my class, but I have a vector of a struct, within that class:

And it's working nice and dandy. I'm up to having 100 of these organisms travelling across the screen, but I've hit a barrier in capabilities (200 organisms and the frame rate drops very noticeably). But that's for another discussion somewhere else

I am curious though, how it is okay that I am doing this, when creating my Vector of 100 Movement variables:

Code:

Movement.push_back(KINETICS());

I did that as a hunch when converting my code. I wasn't aware that structs had constructors...

I believe the only way around this would be to encapsulate the textures in a class and then in that class's destructor call release for the IDirect3DTexture9 interface. Then when the vector goes out of scope in the animation sequence class the COM interfaces would be correctly released because the vector cleanup code would invoke the destructors for the class type of the vector.

Bubba, be careful when creating that "smart pointer" to encapsulate your Texture objects. Anything you put into a vector (or any other STL container) should be copyable, so you would have to have extra handling to know which copy should call Release (assuming calling Release multiple times for the same object is bad). It is a great idea, but you've got to be careful.

The extra code in your destructor to Release all the Images (which is missing an increment by the way) is necessary no matter what kind of container you use. So while it is a serious memory/resource leak with vectors, the vector part is irrelevent. It is just a serious memory/resource leak with IDirect3DTexture9 objects. You also still do not need clear in that case, although I admit that I have put it in there on occasion myself due to habit.

As long as people remember that a vector is not smarter about what is inside it than regular arrays or other normal containers, they should be able to remember to do required cleanup.