Deleting dynamically allocated objects
I'm having a problem deleting objects I've created with the new operator. I have two classes: Particle and ParticleExplosion. ParticleExplosion has:
vector< Particle * > pL
Inside the constructor to the ParticleExplosion class, I make a whole bunch of Particles and put pointers to them in a vector ( pL ):
ParticleExplosion::ParticleExplosion()
{
for( i = 0; i < this->numParticles; ++i )
{
p = new Particle;
pL.push_back( p ); // pL is a vector
}
}
When a ParticleExplosion object gets destroyed (by going out of scope), it's not taking the objects the pL points to with it (which I expected). So, I made my own destructor for ParticleExplosion, where I step through the vector, and delete each item that it poitns to:
ParticleExplosion::~ParticleExplosion()
{
for( int i = pL.size(); i > 0; --i)
{
delete this->pL[i]
}
}
After deleting the first item, the program crashes. Am I going about this the right way?
[email protected]