This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Is gcc allowed to eliminate writes in a destructor?
- From: Mikhail Maltsev <maltsevm at gmail dot com>
- To: Norbert Lange <nolange79 at gmail dot com>, gcc-help at gcc dot gnu dot org
- Date: Tue, 19 May 2015 21:55:33 +0300
- Subject: Re: Is gcc allowed to eliminate writes in a destructor?
- Authentication-results: sourceware.org; auth=none
- References: <CADYdroNwKzUqjg=YJcbJ8Nsjc7NO2EbfeToACvgm-bhHX=ZuYA at mail dot gmail dot com>
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