Newbie STL question

Started by
19 comments, last by bslayerw 22 years, 4 months ago
Hi all, I am having an issue freeing memory using the list from the STL. The problem is when I remove (erase) elements from the list the objects in the list do not get their destructor called, here''s an example: list particleEngines; //I have a function that initialized and adds pointers to objects to the list. void initEngines() { ParticleEngine * p = new ParticleEngine(100); particleEngines.insert(particleEngines.end(),p); } void clearUp() { //Test to see if the memory is getting released particleEngines.erase(particleEngines.begin(),particleEngines.end()); //or particleEngines.clear(); //the destructor for the particlesEngine never get''s called } Could someone elighten me on this issue?
Advertisement
are your destructors declared as virtual?

class ParticleEngine
{
public:
ParticleEngine();
virtual ~ParticleEngine();
};
Yes my destructors are declared virtual. Maybe I should be more specific:

I have a base class:

class ParticleEngine
{
public:

virtual void startup()=0;
virtual void shutdown()=0;
virtual void draw();
virtual void update(float deltaTime)=0;
ParticleEngine(int numParticles = 0);
virtual ~ParticleEngine();
};

//then a sub class:

class ExplosionParticleEngine :public ParticleEngine
{
public:

void startup();
void shutdown();
void update(float deltaTime);
ExplosionParticleEngine(int numParticles);
void restartParticle(int index);
virtual ~ExplosionParticleEngine();

};

//when I add particle eninges to the list I upcast then:
list particleEngines;
void initEngines()
{
ExplosionParticleEngine * p = new ExplosionParticleEngine(100);
particleEngines.insert(particleEngines.end(),(ParticleEngine *)p);

}

void clearUp()
{
//Test to see if the memory is getting released
particleEngines.erase(particleEngines.begin(),particleEngines.end());
//or particleEngines.clear();
//the destructor for the particlesEngine never get's called

}

Oh by the way, how do I wrap my code so that it keeps it format on the forum?



Edited by - bslayerw on November 30, 2001 2:14:30 PM
you have to call delete on the pointer after you remove it from the list

delete p;
The problem here isn''t tha your destructor isn''t getting called, it''s that you''re creating each item twice.

When you use the new operator, you are responsible for explicitly deleting the object at a later time with the delete operator.

I''m also not sure if you want a list of ParticleEngines or a list of pointers to ParticleEngines. If you want the former, this should do the job:





list particleEngines;

//I have a function that initialized and adds pointers to objects to the list.

void initEngines()
{
ParticleEngine p = ParticleEngine(100);
particleEngines.push_back();

}

void clearUp()
{
//Test to see if the memory is getting released
particleEngines.erase(particleEngines.begin(),particleEngines.end());
//or particleEngines.clear();
//the destructor for the particlesEngine never get''s called

}




Visit my web site.
"I came, I saw, I coded."
Visit my web site."I came, I saw, I coded."
You are storing pointers to particle engines, not particle engines. pointers have no destructors. You may want to store auto_ptr instead.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Why would I have to call "delete p". Should it go out of scope once the thread of execution leaves the initEngine() function?

(Excuse my ignorance)
why not create the object without new?

list particleEngines;

void initEngines()
{
ParticleEngine p(100);
particleEngines.insert(particleEngines.end(),p);
}

void clearUp()
{
particleEngines.clear();
}

If I create the object without new will the container become the owner of the object?

I would like the object to be destroyed when removed from the list.
p does go out of scope, but its just a pointer.


The memory allocated by
p = new ParticleEngine(100);

does not go out of scope. You''ve explicitly told the computer to set that memory aside. It won''t give it back to the system until you delete it with the delete operator.

If you lose track of the pointer, the memory remains allocated until the program exits. I''m not sure if all OS''s even give the memory back then... But anything you''re likely to code for will.

Visit my web site.
"I came, I saw, I coded."
Visit my web site."I came, I saw, I coded."

This topic is closed to new replies.

Advertisement