Deleteing Objects from std::vector 
July 23rd, 2005, 02:00 AM
| | | 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? | 
July 23rd, 2005, 02:00 AM
| | | Re: Deleteing Objects from std::vector
<canned.net@gmail.com> wrote in message
news:1110786437.599384.179130@o13g2000cwo.googlegr oups.com...[color=blue]
> 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?[/color]
I donīt see any assert in the code above! What is the assert condition? Iīd
check that.
Chris | 
July 23rd, 2005, 02:00 AM
| | | Re: Deleteing Objects from std::vector
<canned.net@gmail.com> wrote in message
news:1110786437.599384.179130@o13g2000cwo.googlegr oups.com[color=blue]
> 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?[/color]
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 | 
July 23rd, 2005, 02:02 AM
| | | Re: Deleteing Objects from std::vector
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! | 
July 23rd, 2005, 02:02 AM
| | | Re: Deleteing Objects from std::vector
<canned.net@gmail.com> wrote in message
news:1110831082.751395.35330@g14g2000cwa.googlegro ups.com[color=blue]
>
> 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?[/color]
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 | 
July 23rd, 2005, 02:03 AM
| | | Re: Deleteing Objects from std::vector
On 3/14/2005 2:11 PM, canned.net@gmail.com wrote:[color=blue]
> 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![/color]
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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|