[PATCH] D13080: [libc++] ~unique_ptr() should not set stored pointer to null

Daniel Cheng via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 22 17:06:28 PDT 2015


dcheng created this revision.
dcheng added a reviewer: thakis.
dcheng added a subscriber: cfe-commits.

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

http://reviews.llvm.org/D13080

Files:
  include/memory
  test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.dtor/stored_pointer.pass.cpp

Index: test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.dtor/stored_pointer.pass.cpp
===================================================================
--- /dev/null
+++ test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.dtor/stored_pointer.pass.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// unique_ptr
+
+// ~unique_ptr() should not set the stored pointer to nullptr.
+
+#include <memory>
+#include <cassert>
+
+struct A;
+
+struct B
+{
+    std::unique_ptr<A> a;
+};
+
+struct A
+{
+    B* b;
+   ~A() {assert(b->a);}
+};
+
+int main()
+{
+    B b;
+    b.a.reset(new A);
+    b.a->b = &b;
+}
Index: include/memory
===================================================================
--- include/memory
+++ include/memory
@@ -2696,7 +2696,11 @@
             {reset(__p.release()); return *this;}
 
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
-    _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
+    _LIBCPP_INLINE_VISIBILITY ~unique_ptr()
+    {
+        if (__ptr_.first() != pointer())
+            __ptr_.second()(__ptr_.first());
+    }
 
     _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
     {
@@ -2888,7 +2892,11 @@
     }
 
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
-    _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
+    _LIBCPP_INLINE_VISIBILITY ~unique_ptr()
+    {
+        if (__ptr_.first() != pointer())
+            __ptr_.second()(__ptr_.first());
+    }
 
     _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
     {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13080.35446.patch
Type: text/x-patch
Size: 1900 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150923/581ade1d/attachment.bin>


More information about the cfe-commits mailing list