473,470 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 12246

<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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.