Deleteing Objects from std::vector

C

canned.net

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;
}

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?
 
C

Chris Theis

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;
}

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
 
J

John Carson

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;
}

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;
v.clear();

return 0;
}
 
C

canned.net

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!
 
K

Kristo

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
 
J

John Carson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top