A little problem with std::vetor

Started by
11 comments, last by rip-off 17 years, 6 months ago
Hi everyone! I've got two classes: class CObject { public: char* Name; D3DXVECTOR3 Position; CObject(); ~CObject(); }; class CObjectHandler { private: vector<CObject>* objectList; public: void AddObject(char* Name, D3DXVECTOR3 Position); void DeleteAllObjects(); CObjectHandler(); ~CObjectHandler(); }; And my problem is within the "DeleteAllObjects" function, because I don't know how I can delete those "classes" from the vector. I always get an "debug assertion failed!"-error when I want to quit my game. I tested something like this: void CObjectHandler::DeleteAllObjects() { for(unsigned int i = 0; i < this->objectList->size(); i++) { // something needs to be done here... } this->objectList->clear(); } So, just clearing the list doesn't work. Has anybody an idea what I have to do to "release"/delete the classes from the vector??? Please help! Thank you very much for any help!
Advertisement
Is CObjects destructor virtual? I assume it has subclasses.

Do you call delete on all the CObjects in the vector, as part of the "something needs to be done here..." bit?

More code please. [smile]

Also note that the use of this-> before all members is not needed and IMO harder to read.
Quote:vector<CObject>* objectList;


Important question: why are you using a pointer to a vector?

As for your actual question: clearing a vector destroys all the objects it contains. You can see this by yourself, if you want, by placing a breakpoint in the destructor of your objects. So, your problem lies somewhere else.

Quote:Do you call delete on all the CObjects in the vector


Calling delete on the instances owned by a vector is an extremely bad idea.
Well my question is what I have to write instead of "something needs to be done here..."! In another project where I had "structs" in the vector, I used to run through the list and releasing something I can't remember now (think this was a texture).

I use a pointer to a vector, because I thought to create everything dynamically ( == on the heap???). If you can explain why you wouldn't do so, you probably can convince me to change it.
Quote:Original post by stefan5000I use a pointer to a vector, because I thought to create everything dynamically ( == on the heap???). If you can explain why you wouldn't do so, you probably can convince me to change it.


Can you explain why you want to do so?
Quote:Original post by stefan5000
Well my question is what I have to write instead of "something needs to be done here..."! In another project where I had "structs" in the vector, I used to run through the list and releasing something I can't remember now (think this was a texture).


If you own a vector that contains pointers, and you are the owner of pointed-to memory, then you might have to delete them all before clearing the vector, or risk a memory leak.

In your case, there are no ownership issues: the vector owns everything, and cleans everything up when it's cleared.

Quote:I use a pointer to a vector, because I thought to create everything dynamically ( == on the heap???). If you can explain why you wouldn't do so, you probably can convince me to change it.


I wouldn't do so because it provides nothing substantial (the static portion of a vector is pretty light, so allocating it as part of a class is not really problematic) while increasing the chance of error (you have to delete it, make sure that it has been initialized, and take care of ownership problems if your class gets copied) and the initialization time (since it requires an additional allocation).

So, I would write std::vector<Object> objects, or maybe std::auto_ptr<std::vector<Object> > objects if the class size was really that important, but certainly not a naked pointer.

Well my current understanding is something like this:

// vector is created on stack (== small)
vector<CObject> objectList;

// vector is created on heap (== big)
vector<CObject>* objectList = new vector<CObject>;
...
delete objectList;

Am I totally wrong?
Quote:Original post by stefan5000
Am I totally wrong?


Yes. The static size of a vector (the thing that matters when you allocate it) is always the same, and it's pretty small (between 12 and 20 bytes, generally). There is really no point in allocating it dynamically.

The dynamic memory of a vector (the place where objects are stored) is handled internally by the vector, and placed where it makes the most sense, without requiring your intervention.

"In your case, there are no ownership issues: the vector owns everything, and cleans everything up when it's cleared."

well...but why then this error when quitting?

There is no problem, when there is no object in the list, but when I have created one, this error comes up when "DeleteAllObjects" is called and in this function is nothing else than "objectList->clear();"
This would indicate that there is a bug in your CObject destructor. Can you pinpoint the exact line of code where everything breaks down, using a debugger?

This topic is closed to new replies.

Advertisement