top | item 8697484

(no title)

eclipsor | 11 years ago

If the destructor isn't implemented in derived classes, they will use the parent's destructor. Not a guaranteed memory leak..... but something you need to be aware of.

discuss

order

nostrademons|11 years ago

It's worse than that - if you ever have a a variable with the static type of the base class but the dynamic type of a derived class, memory will leak, even if you've implemented the destructor in derived classes. For example, this code:

  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.)