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
     {


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
     {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to