Relatively simple question about handling destructors properly...
First I've got a class that's something like this:
class Foo {
public:
ReleaseObjects() {
for (std::map<size_t, Object*>::iterator iter = objects.begin(); iter != objects.end(); iter++) {
delete (*iter).second;
}
objects.clear();
}
private:
std::map<size_t,Object*> objects;
}
So the function simply deletes the objects, which were created using 'new'. Problem is an Object class:
class Bar : public Object {
public:
Bar() {
baz = new Baz();
}
~Bar() { delete baz; }
private:
Baz* baz;
}
If I add a type Baz object to Foo, and then try to ReleaseObjects(), I get a memory leak (valgrind). The issue points to baz being leaked, and I guess that means the destructor in bar is never called? So what I'd like to know is how to call the Bar destructor when trying to destroy that object (I can't alter the Bar class, but I can alter Foo).
Edit: Oops, sorry for the syntax errors. Anyways, thanks for all the replies, silly me forgot to implement a proper destructor in my Baz class! Oh, and Baz is actually a template class, but I figured Baz was sort of irrelevant to my question, and that the problem was the destructor in Bar not being called... well, I was wrong, problem is in Baz after all. But thanks again, I think I got it figured out from here!