When a particle is deleted during the update() you need to specify that s_Particle no longer contains a valid pointer to object. You can do this using automated memory management with smart pointers (the example below uses std::shared_ptr from C++0x) or by setting the variable to 0 or NULL (or nullptr in C++0x). Setting the pointer to 0 after deleting will prevent invalid deletions of unallocated memory at a later point since delete(0) safely resolves to a no-op.
std::shared_ptr Version
Code: Select all
#include <memory>
class Particle_Emitter : Object
{
private:
...
// member variables
std::shared_ptr<Particle> s_Particle; // a single particle
...
public:
/* ||||||| DESTRUCTOR ||||||||| */
~Particle_Emitter();
Code: Select all
Particle_Emitter::Particle_Emitter(const unsigned int type, const unsigned int density)
{
.....
allocate_memory_particle(s_Type);
init_particles();
}
Code: Select all
void Particle_Emitter::allocate_memory_particle(const unsigned int type)
{
if(type == CIRCLE)
{
std::shared_ptr<Particle> qp(new Particle(0.0f, 2*(float)M_PI));
s_Particle = qp;
}
}
Code: Select all
Particle_Emitter::~Particle_Emitter()
{
// Do Nothing Here Since shared_ptr Deallocates When Reference Count Is Zero
}
Code: Select all
void Particle_Emitter::deallocate_memory_particle()
{
// Do Nothing Here Since shared_ptr Deallocates When Reference Count Is Zero
}
Code: Select all
void Particle_System::update()
{
std::vector<Particle_Emitter>::iterator it; // Iterator
for(it=o_Particle_Container.begin();it < o_Particle_Container.end();)
{
if(it->s_flag_onscreen == false && it->get_Repeat() == false)
{
// erase returns a pointer to the next object in the STL container
it->deallocate_memory_particle();
it = o_Particle_Container.erase(it);
}
// we only want to add to it if we don't delete an object, or we will add twice when we delete an object.
else
{
it++;
}
}
}
--------------------------------------------------------------------------------------------
Regular Pointer Version
Code: Select all
class Particle_Emitter : Object
{
private:
...
// member variables
Particle *s_Particle; // a single particle
...
public:
/* ||||||| DESTRUCTOR ||||||||| */
~Particle_Emitter();
Code: Select all
Particle_Emitter::Particle_Emitter(const unsigned int type, const unsigned int density)
: s_Particle(0)
{
.....
allocate_memory_particle(s_Type);
init_particles();
}
Code: Select all
void Particle_Emitter::allocate_memory_particle(const unsigned int type)
{
if(type == CIRCLE)
{
Particle *qp = new Particle(0.0f, 2*(float)M_PI);
s_Particle = qp;
}
}
Code: Select all
Particle_Emitter::~Particle_Emitter()
{
if(s_Particle) delete s_Particle;
}
Code: Select all
void Particle_Emitter::deallocate_memory_particle()
{
if(s_Particle) delete s_Particle;
s_Particle = 0;
}
Code: Select all
void Particle_System::update()
{
std::vector<Particle_Emitter>::iterator it; // Iterator
for(it=o_Particle_Container.begin();it < o_Particle_Container.end();)
{
if(it->s_flag_onscreen == false && it->get_Repeat() == false)
{
// erase returns a pointer to the next object in the STL container
it->deallocate_memory_particle();
it = o_Particle_Container.erase(it);
}
// we only want to add to it if we don't delete an object, or we will add twice when we delete an object.
else
{
it++;
}
}
}