f



Deleteing Objects from std::vector

I have a class Scene that has several subclasses:  World, Vault, etc.

I fill a vector with these classes and then cannot go through and
delete them.  What's the trick to deleting pointers from a vector?

Code:

std::vector<Scene*> scenes;

void initializeGame()
{
	robot = new Robot("COM2");
	player = new Player();

	//Add Scenes
	scenes.push_back(new World(robot, player));
	scenes.push_back(new SecurityWires(robot));
	scenes.push_back(new Keypad(robot));
	scenes.push_back(new Maze(robot, player));
	scenes.push_back(new Explosives(robot, player));
	scenes.push_back(new Vault(robot));
	scenes.push_back(new Map());
}

void endGame()
{
        for(int i = 0; i < scenes.size(); i++)
        {
		delete scenes[i];
        }

        scenes.clear();
}

When my program starts, I call intializeGame.  When I go to exit the
game, I call endGame() and I get an assertion failure.  Any ideas?

0
3/14/2005 7:47:17 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

5 Replies
237 Views

Similar Articles

[PageSpeed] 10

<canned.net@gmail.com> wrote in message
news:1110786437.599384.179130@o13g2000cwo.googlegroups.com...
> I have a class Scene that has several subclasses:  World, Vault, etc.
>
> I fill a vector with these classes and then cannot go through and
> delete them.  What's the trick to deleting pointers from a vector?
>
> Code:
>
> std::vector<Scene*> scenes;
>
> void initializeGame()
> {
> robot = new Robot("COM2");
> player = new Player();
>
> //Add Scenes
> scenes.push_back(new World(robot, player));
> scenes.push_back(new SecurityWires(robot));
> scenes.push_back(new Keypad(robot));
> scenes.push_back(new Maze(robot, player));
> scenes.push_back(new Explosives(robot, player));
> scenes.push_back(new Vault(robot));
> scenes.push_back(new Map());
> }
>
> void endGame()
> {
>         for(int i = 0; i < scenes.size(); i++)
>         {
> delete scenes[i];
>         }
>
>         scenes.clear();
> }
>
> When my program starts, I call intializeGame.  When I go to exit the
> game, I call endGame() and I get an assertion failure.  Any ideas?

I don�t see any assert in the code above! What is the assert condition? I�d
check that.
Chris


0
3/14/2005 8:34:12 AM
<canned.net@gmail.com> wrote in message
news:1110786437.599384.179130@o13g2000cwo.googlegroups.com
> I have a class Scene that has several subclasses:  World, Vault, etc.
>
> I fill a vector with these classes and then cannot go through and
> delete them.  What's the trick to deleting pointers from a vector?
>
> Code:
>
> std::vector<Scene*> scenes;
>
> void initializeGame()
> {
> robot = new Robot("COM2");
> player = new Player();
>
> //Add Scenes
> scenes.push_back(new World(robot, player));
> scenes.push_back(new SecurityWires(robot));
> scenes.push_back(new Keypad(robot));
> scenes.push_back(new Maze(robot, player));
> scenes.push_back(new Explosives(robot, player));
> scenes.push_back(new Vault(robot));
> scenes.push_back(new Map());
> }
>
> void endGame()
> {
>        for(int i = 0; i < scenes.size(); i++)
>        {
> delete scenes[i];
>        }
>
>        scenes.clear();
> }
>
> When my program starts, I call intializeGame.  When I go to exit the
> game, I call endGame() and I get an assertion failure.  Any ideas?

What is the assertion that fails?

There is no trick to deleting pointers from a vector. For example, the 
following runs without any problems. The fact that your code doesn't run 
correctly suggests that the problem is in code that you haven't shown us. 
Where do robot and player come from, for example, and when are they 
destructed? What is the destructor code for your derived classes?

In the example below, I have given Base a virtual destructor. This makes no 
difference in my example, but is necessary in general if you want the 
derived class destructors to be called.

#include <vector>

class Base
{
public:
virtual ~Base()
{}
};

class Derived1 : public Base
{
};

class Derived2 : public Base
{
};

int main()
{
    std::vector<Base *> v;
    v.push_back(new Derived1);
    v.push_back(new Derived2);

    for (size_t i=0; i<v.size(); ++i)
        delete v[i];
    v.clear();

    return 0;
}


-- 
John Carson 

0
3/14/2005 8:42:18 AM
Alright, Got it working.  Thanks for the help guys.  It turns out I
just didn't quite fully understand destructors and inheritance in C++.
I'm a university student and all they do is shove Java down our
throats.  ::hides in corner::.

Anyway, the first problem was that I didn't have my parent class
destructors set up as virtual.  Then my next problem was that in my
child class destructors, I was calling the parent class destructors.
Now, since this is done automatically, it would get done twice (once
with my call, once automatically), obviously causing problems.  Now it
seems that everything is working.

One more question.  If you have a string, char * s = "Hello World";
What do you do to clean that up.  Do you: delete[] s; Or just: delete
s; ?  Or something else all together?

Thanks again!

0
3/14/2005 8:11:22 PM
On 3/14/2005 2:11 PM, canned.net@gmail.com wrote:
> One more question.  If you have a string, char * s = "Hello World";
> What do you do to clean that up.  Do you: delete[] s; Or just: delete
> s; ?  Or something else all together?
> 
> Thanks again!

You don't have to do anything.  It will be cleaned up for you. 
Remember, if you didn't new it, don't delete it.

Kristo

0
kristo605 (99)
3/14/2005 9:30:53 PM
<canned.net@gmail.com> wrote in message
news:1110831082.751395.35330@g14g2000cwa.googlegroups.com
>
> One more question.  If you have a string, char * s = "Hello World";
> What do you do to clean that up.  Do you: delete[] s; Or just: delete
> s; ?  Or something else all together?

You don't do anything. You call delete if and only if you have allocated 
memory with new. Everything else it cleaned up automatically.


-- 
John Carson 

0
3/14/2005 9:34:57 PM
Reply: