Sorry if this is an ignorant question, but can you elaborate on this? It's not clear to me why this would lead to memory leaks.
class B : public A { ... } void foo() { A* someA = new B(...); ... delete someA; }
if A destructor is not virtual, then B's resources leak.
http://stackoverflow.com/questions/461203/when-to-use-virtua...
class Base {
public:
Base() {}
~Base() {}
};
class Derived : public Base {
private:
Foo* _foo;
public:
Derived() : _foo(new Foo) {}
~Derived() { delete _foo; }
};
int main() {
std::unique_ptr<Base>(new Derived);
}
The problem here is that if the destructor is not marked virtual, then when unique_ptr<Base> goes to invoke the destructor, it calls ~Base instead of ~Derived. That has no knowledge of Derived's member variables, so any memory that Derived allocated leaks. (Technically, it's undefined behavior and can do whatever it wants, but most implementations in practice will call ~Base.)