# A little problem with std::vetor

This topic is 4091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
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?

Also note that the use of this-> before all members is not needed and IMO harder to read.

##### Share on other sites
Quote:
 vector* 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.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by stefan5000Well 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.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by stefan5000Am 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.

##### Share on other sites
"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();"

##### Share on other sites
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?

##### Share on other sites
OMG!!! Thanks for this tip!

Here is my Game.exe-code...

#include "main.h"int WINAPI WinMain(HINSTANCE hInstance,				   HINSTANCE hPrevInstance,				   LPSTR lpCmdLine,				   int nCmdShow){CMain* main = new CMain;CEngine32* engine = new CEngine32;engine->StartUp("Game",		640,		480,		0,		0);SetCursorPos((*engine->GetD3DPPPointer()).BackBufferWidth/2,	     (*engine->GetD3DPPPointer()).BackBufferHeight/2);engine->pObjects->AddObject("Object1", D3DXVECTOR3(0.0f, 0.0f, 0.0f));		//Render()...	engine->Exit(); // expected the error in here, BUT...delete engine; // it's right when executing this line...I take a look at it againdelete main;return 0;}

##### Share on other sites
Oh! I'm so sorry!!!

My mistake was that in the CObjectHandler's destructor I tried to delete a member pointer of type DWORD*. But somehow this haven't worked!

Nevertheless: Thanks a lot!!! (for the tips with the pointer and everything else)

I'm sorry to waste your time!

##### Share on other sites
Quote:
Original post by ToohrVyk

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.

As you may have guessed, I misread his code, I thought it was a vector of CObject pointers, not a pointer to a vector of CObjects [embarrass]

##### Share on other sites

This topic is 4091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628703
• Total Posts
2984305

• 23
• 10
• 9
• 13
• 13