This is an archive of the discontinued LLVM Phabricator instance.

[libc++] ~unique_ptr() should not set stored pointer to null
AbandonedPublic

Authored by dcheng on Sep 22 2015, 5:06 PM.

Details

Reviewers
mclow.lists
Summary

This better matches the behavior of MSVC and libstdc++: neither
standard library sets the stored pointer to null when destroying the
unique_ptr.

Diff Detail

Event Timeline

dcheng updated this revision to Diff 35446.Sep 22 2015, 5:06 PM
dcheng retitled this revision from to [libc++] ~unique_ptr() should not set stored pointer to null.
dcheng updated this object.
dcheng added a reviewer: thakis.
dcheng added a subscriber: cfe-commits.

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?

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.

thakis edited reviewers, added: mclow.lists; removed: thakis.Sep 22 2015, 5:50 PM
thakis added a subscriber: thakis.

mclow should review this, not me.

mclow.lists edited edge metadata.Sep 23 2015, 8:12 AM

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.

dcheng abandoned this revision.Sep 23 2015, 2:56 PM

OK, thanks for highlighting the proposed resolution.