473,883 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ 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<Sce ne*> scenes;

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

//Add Scenes
scenes.push_bac k(new World(robot, player));
scenes.push_bac k(new SecurityWires(r obot));
scenes.push_bac k(new Keypad(robot));
scenes.push_bac k(new Maze(robot, player));
scenes.push_bac k(new Explosives(robo t, player));
scenes.push_bac k(new Vault(robot));
scenes.push_bac k(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 12282

<ca********@gma il.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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<Sce ne*> scenes;

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

//Add Scenes
scenes.push_bac k(new World(robot, player));
scenes.push_bac k(new SecurityWires(r obot));
scenes.push_bac k(new Keypad(robot));
scenes.push_bac k(new Maze(robot, player));
scenes.push_bac k(new Explosives(robo t, player));
scenes.push_bac k(new Vault(robot));
scenes.push_bac k(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********@gma il.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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<Sce ne*> scenes;

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

//Add Scenes
scenes.push_bac k(new World(robot, player));
scenes.push_bac k(new SecurityWires(r obot));
scenes.push_bac k(new Keypad(robot));
scenes.push_bac k(new Maze(robot, player));
scenes.push_bac k(new Explosives(robo t, player));
scenes.push_bac k(new Vault(robot));
scenes.push_bac k(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<Bas e *> 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********@gma il.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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********@gmai l.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
6023
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
2893
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 contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
20
17876
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
17
3371
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 this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
8
5125
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
32
69728
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: std::vector<int> fun(){ //build the vector v; return v; }
56
5851
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
2969
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
10773
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 there an existing adapter? Thanks, Daniel.
0
9933
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9789
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11129
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7967
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.