I have a class called ClassA. This ClassA holds a vector of pointers to dynamically allocated objects of class ClassB. The destructor for ClassA looks like this:
ClassA::~ClassA() {
for (vector<ClassB*>::iterator i = m_vActiveB.begin(), e = m_vActiveB.end(); i != e; ) {
vector<ClassB*>::iterator tmp(i++);
delete *tmp;
m_vActiveB.erase(tmp);
}
}
In ClassC, I have an vector of ClassA objects. My current scenario is this: if I don't have any ClassA objects in my ClassC vector, then it all works fine. But if I have objects in there, and then call .erase() on my vector in ClassC, then an exception is thrown. Through logging I have determined that the ClassA destructor is called (as it should be, as my understanding is that .erase() destroys each vector member), but the exception is thrown when the loop begins.
Does anyone know what might be causing this? Thanks!
shared_ptrandunique_ptrare your friends, if you use one of them, you don't even have to iterate the vector destroying each of the members. - Massa