Is the whole object freed with a non-virtual destructor and a Base class pointer?

It’s undefined behavior, so anything can happen. Quote from the standard [expr.delete]:

In the first alternative (delete object), if the static type of the object
to be deleted is different from its
dynamic type, the static type shall be
a base class of the dynamic type of
the object to be deleted and the
static type shall have a virtual
destructor or the behavior is
undefined.

Just don’t do that.


I think it’s a good idea to understand what actually happens though to understand why the standard has that requirement.

In your simple case, on a typical implementation, the memory will be freed regardless. That is because operator delete will call free(p). Since Base is the first non-virtual base class of Derived, it happens to be located at the beginning of the allocated memory block. And since free must know the size of the allocated block by its own bookkeeping (being a C function it doesn’t know anything about sizes of types), it will deallocate the entire block.

However, since Base doesn’t have a virtual destructor, delete p is resolved based on the static-type of *p (which is Base). Consequently, as you’re seemingly aware, it will not call the destructor of Derived. If Derived had any non-trivial members or base-classes, those would not be destructed either, so any resources that they manage would be leaked.

When the class has a virtual destructor, the work of freeing the memory is delegated to the destructor. The reason is that, although free knows to determine the size of the block, it would still need to have the pointer to the beginning of it. Base can be at an arbitrary offset within Derived in the general case, therefore Derived destructor will be responsible for adjusting the pointer prior to freeing it. This also adds the need for a separate destructor that would destructor the object without actually freeing the memory; e.g. as a subobject of another derived type, or as explicit destructor call. And let’s keep delete[] out of the discussion. When you mark your destructor as virtual, the compiler takes care of all this for you.

The bottom line is that the standard doesn’t describe all these implementation details, leaving them instead to—um—the implementation. They could formulate some sensible criteria when deleting a non-virtual base would still be ok; but instead they went with the simple clear-cut “you have a virtual destructor then it’s ok, otherwise it’s not ok” rule.

Leave a Comment