Deallocate vector of pointers

Jan 24, 2011 at 7:51pm
Here is the scenario:

I have a Mesh class that is abstract and then I have a Cube, Sphere, Cone, and Cylinder class that inherit from Mesh.

1
2
3
vector<Mesh*> m;

m.push_back(new Cube());


How do I deallocate the memory created by new?
Jan 24, 2011 at 8:41pm
1
2
3
4
5
while(!m.empty())
{
  delete m.back();
  m.pop_back();
}


Or... use a boost::ptr_vector instead, and it will do it automatically (but of course, then you need boost)
Jan 24, 2011 at 9:13pm
Thanks for the reply. When I implement the code, I get this error:

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
Last edited on Jan 24, 2011 at 9:14pm
Jan 25, 2011 at 5:22am
I found the problem. I was trying to deallocate the memory twice. I did not check in the object destructors for NULL pointers.
Topic archived. No new replies allowed.