[cfe-dev] is delete on abstract with non-virtal ever safe?
Sebastian Redl
sebastian.redl at getdesigned.at
Mon Jul 23 11:53:28 PDT 2012
On 23.07.2012, at 16:29, Henry Miller wrote:
>
> I understand (and agree with) the warning, however the owner of this
> code argues that only the default destructors are used, and they have
> procedures in place to ensure that no derived class will ever have
> members that need destruction. So the question is he correct that the
> destructor for derived doesn't need to be called?
Here are some things that don't work correctly if you fail to provide a virtual destructor:
- Destruction of members in derived classes. (You say that is never necessary.)
- If the derived class has other bases besides base, and base is not the very first in the object layout, or else if base is a virtual base class (or part of a virtual base class), the code will pass the wrong pointer to operator delete.
- If you overload operator new and delete for some derived class, you won't get the correct operator delete called.
- For that matter, if you overload operator new and delete in the base class, and you use the pointer+size version of operator delete, the passed size will be incorrect.
There may be more, those are just those I can think of off the top of my head.
Sebastian
More information about the cfe-dev
mailing list