Hi. I am using an std::Vector and I have a question about memory allocation.
I have a vector of animation objects inside my graphic object. I add new animations externally via this method:
void Graphic::addAnimation( int* frames, AnimationStyle style, int frameCount, double updateInterval)
{
Animation* a = new Animation(frames, style, frameCount, updateInterval);
animations_.push_back(a);
}
Now, when I call the destructor should I do it this way...
Graphic::~Graphic()
{
// Free all animations that have been allocated for this graphic.
for ( unsigned int i = 0; i < animations_.size(); i++)
{
Animation* a = animations_[i];
delete a;
}
}
or this way...
Graphic::~Graphic()
{
// Free all animations that have been allocated for this graphic.
for ( unsigned int i = 0; i < animations_.size(); i++)
{
animations_.pop_back();
}
}
All I need to do is call delete and give it the address of the object I am deleting right? I'm pretty sure with the first way I will end up with a vector full of dead pointers, but will the second way result in a memory leak or will the pop_back delete the memory for me?