Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

nitzan

deleting pointers from an STL vector

This topic is 5396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an stl vector of pointers to objects: vector models; Then I add objects to it: for(int i=0; i<10; i++) { Model * mymodel = new Model(); models.push_back(model); } Now I want to delete the vector models and all its objects. How do I do this ? Simply calling erase doesnt actually delete the object. I tried iterating through the vector, but I cant seem to get the iterator to delete the object and erase it from the vector. I keep running into previously freed memory. Thanks, Nitzan

Share this post


Link to post
Share on other sites
Advertisement
Because the type Model* obviously doesn''t have a destructor, you must call the delete operator on the elements of models (the pointer-to-Models). Only after you have returned the dynamically allocated memory (i.e. call delete on the pointer) should you erase the Model* element from the container. Here is a nice clean way:

while ( !models.empty() )
{
delete models.back(); // call delete on Model* object

models.pop_back(); // erase from container (vector)

}


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites

struct Gomez
{
template<typename T>
void operator()(T* p) {delete p;}
};

std::for_each(models.begin(), models.end(), Gomez());
models.clear();


[edited by - Magmai Kai Holmlor on August 17, 2003 11:07:22 PM]

Share this post


Link to post
Share on other sites
Magmai Kai Holmlor - that is some sexy code. To be honest, it completely skipped my mind...the whole templated function optimization thing....that''s...oh....so sexy.

Share this post


Link to post
Share on other sites
ProgramMax: I don''t quite see how it''s "sexy", but okay. They are termed function objects, and are commonly used with the STL. They are very efficient and useful. Take a look here.

I would have used one, but I thought it was too complex for someone who couldn''t work out how to free the dynamically allocated memory in their vector.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
somehow i think yours is more sexy, lektrix :D

its so simple, and something i never actually throught of.. (thought, i use that regularly in queues or stacks:D)

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!