Re:Data structures and Algorithms in C/C++11
by nostrademons on Wednesday December 03, 2014 @10:18PM
(#8697689)
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:
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.)
[ Parent
| on HN ]
There are no comments on this comment yet.