472,374 Members | 1,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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?

Jul 23 '05 #1
5 12194

<ca********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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
Jul 23 '05 #2
<ca********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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

Jul 23 '05 #3
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!

Jul 23 '05 #4
<ca********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.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

Jul 23 '05 #5
On 3/14/2005 2:11 PM, ca********@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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.