[cfe-dev] ::delete behaves differently in clang and gcc when class has a virtual destructor

Sebastian Redl sebastian.redl at getdesigned.at
Mon Jul 11 00:59:43 PDT 2011


On 06.07.2011 01:26, Patrick Pelletier wrote:
> First of all, here are the assumptions I'm starting with:
>
> a) the "delete" expression calls the object's destructor, followed by
> calling "operator delete" on the object
>
> b) "::delete" is the same as "delete", except that instead of calling
> "operator delete", it calls "::operator delete"
>
> If these assumptions are correct, then in the program below, I would
> expect case 1 to call Foo's operator delete, but I would not expect case
> 2 or case 3 to call Foo's operator delete.  (Indeed, based on my
> assumptions above, I would expect case 2 and case 3 to be identical; my
> belief is that case 3 is just a manual de-sugaring of case 2.)
Exception handling is different in case 2 than 3, and null pointers are 
treated differently, but for the simple case, that's correct.
> When compiled with gcc, the program below behaves as I would expect.
> However, when compiled with clang, the program as written (i. e. with
> the virtual destructor) does not behave as I expect, in that Foo's
> operator delete is called in case 2.  (Although still not in case 3.)
>
> The presence of the virtual destructor is important.  If I simply remove
> the word "virtual" from the declaration of ~Foo(), then the program
> behaves identically in clang and gcc, and both are what I expect.  (i.
> e. case 1 is the only case that calls Foo's operator delete.)
>
> Thanks for any light you can shed on this behavior, and my apologies if
> this is indeed expected behavior that I am unaware of.
No, I believe you found a genuine bug. Please file it.

Sebastian



More information about the cfe-dev mailing list