This better matches the behavior of MSVC and libstdc++: neither
standard library sets the stored pointer to null when destroying the
unique_ptr.
Details
- Reviewers
mclow.lists
Diff Detail
Event Timeline
I'd still be curious to see a stronger justification from the standard
about this.
I know you mentioned/quoted the definition of ~unique_ptr in the standard -
but I'm not sure that is meant to imply observable behavior during
destruction (though I could be wrong). Is there anything in the standard
about accessing objects while their dtors are executing?
From 3.8.5 [basic.life]:
Before the lifetime of an object has started but after the storage which the object will occupy has been
allocated or, after the lifetime of an object has ended and before the storage which the object occupied is
reused or released, any pointer that refers to the storage location where the object will be or was located
may be used but only in limited ways. For an object under construction or destruction, see 12.7.
From 12.7.1 [class.dtor]:
For an object with a non-trivial destructor, referring to any non-static member or base class of the object after the destructor finishes execution results in undefined behavior.
So I believe the standard permits object access while the dtor is still executing.
Please DO NOT commit this patch.
See http://cplusplus.github.io/LWG/lwg-active.html#2224 for a discussion of this code, and the (proposed) resolution is that this is undefined behavior.