Is gcc allowed to eliminate writes in a destructor?
Mikhail Maltsev
maltsevm@gmail.com
Tue May 19 18:47:00 GMT 2015
On 05/19/2015 08:31 PM, Norbert Lange wrote:
> Hello,
>
> I know that using a class after a destructor is called is a nasty
> thing and I already changed the code, but I am curious if gcc is
> allowed by the standard to eliminate stores to member variables.
>
> I had code similar to the following:
>
> Destructor:
> if (_pAlloc) free(_pAlloc);
> _pAlloc = 0;
> _SomeData = 0;
>
> what happened was that I explicitly called the destructor for
> "cleanup" in another method. The free was called, and after the
> instance went out of scope it was called again with the same address.
Yes, that is allowed, because your program invokes undefined behavior.
According to ISO C++11 standard:
12.4. <...> "Once a destructor is invoked for an object, the object no
longer exists; the behavior is undefined if the destructor is invoked
for an object whose lifetime has ended (3.8). [ Example: if the
destructor for an automatic object is explicitly invoked, and the block
is subsequently left in a manner that would ordinarily invoke implicit
destruction of the object, the behavior is undefined. â end example ]"
--
Regards,
Mikhail Maltsev
More information about the Gcc-help
mailing list