[cfe-dev] is delete on abstract with non-virtal ever safe?

Jonathan Sauer jonathan.sauer at gmx.de
Fri Oct 4 07:44:46 PDT 2013


Hello,

>> This has undefined behavior. You're calling a virtual function on an object
> whose lifetime has ended (the destructor has already been called).
> 
> my_destroy_interface has no destructor so the vtable is not cleared until

That is an implementation detail, though (just as are vtables).

> the memory is released (since nothing alters it before i get in the operator
> delete overload).
> 
> Referring to the standards (i'm not sure its the right pdf):
> "The lifetime of an object of type T ends when:
> — if T is a class type with a non-trivial destructor (12.4), the destructor
> call starts, or
> — the storage which the object occupies is reused or released."
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
> 
> And that make totally sense since if i add a  (non-virtual) destructor it
> still flies, unless the destructor is non trivial (in that case i get as
> expected a pure virtual function call).

But the standard talks about *trivial* destructors, not *virtual* ones.
>From §12.4p5:

| A destructor is trivial if it is not user-provided and if:

So the moment you define a destructor you have a non-trivial one.

Unless I'm missing something.


Jonathan





More information about the cfe-dev mailing list