| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 StructA* a = new StructA; | 662 StructA* a = new StructA; |
| 663 a->b.reset(new StructB); | 663 a->b.reset(new StructB); |
| 664 a->b->a.reset(a); | 664 a->b->a.reset(a); |
| 665 | 665 |
| 666 // Break the cycle by calling reset(). This will cause |a| (and hence, |a->b|) | 666 // Break the cycle by calling reset(). This will cause |a| (and hence, |a->b|) |
| 667 // to be deleted before the call to reset() returns. This tests that the | 667 // to be deleted before the call to reset() returns. This tests that the |
| 668 // implementation of scoped_ptr::reset() doesn't access |this| after it | 668 // implementation of scoped_ptr::reset() doesn't access |this| after it |
| 669 // deletes the underlying pointer. This behaviour is consistent with the | 669 // deletes the underlying pointer. This behaviour is consistent with the |
| 670 // definition of unique_ptr::reset in C++11. | 670 // definition of unique_ptr::reset in C++11. |
| 671 a->b.reset(); | 671 a->b.reset(); |
| 672 | |
| 673 // Go again, but this time, break the cycle by invoking |a|'s destructor. This | |
| 674 // tests that the implementation of ~scoped_ptr doesn't infinitely recurse | |
| 675 // into the destructors of |a| and |a->b|. Note, deleting |a| instead will | |
| 676 // cause |a| to be double-free'd because |a->b| owns |a| and deletes it via | |
| 677 // its destructor. | |
| 678 a = new StructA; | |
| 679 a->b.reset(new StructB); | |
| 680 a->b->a.reset(a); | |
| 681 a->~StructA(); | |
| 682 } | 672 } |
| 683 | 673 |
| 684 TEST(ScopedPtrTest, Operators) { | 674 TEST(ScopedPtrTest, Operators) { |
| 685 struct Parent {}; | 675 struct Parent {}; |
| 686 struct Child : public Parent {}; | 676 struct Child : public Parent {}; |
| 687 | 677 |
| 688 scoped_ptr<Parent> p(new Parent); | 678 scoped_ptr<Parent> p(new Parent); |
| 689 scoped_ptr<Parent> p2(new Parent); | 679 scoped_ptr<Parent> p2(new Parent); |
| 690 scoped_ptr<Child> c(new Child); | 680 scoped_ptr<Child> c(new Child); |
| 691 scoped_ptr<Parent> pnull; | 681 scoped_ptr<Parent> pnull; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 848 | 838 |
| 849 if (null_ptr) { | 839 if (null_ptr) { |
| 850 ADD_FAILURE() << "Null pointer should result in false."; | 840 ADD_FAILURE() << "Null pointer should result in false."; |
| 851 } | 841 } |
| 852 | 842 |
| 853 if (!null_ptr) { // check for operator!(). | 843 if (!null_ptr) { // check for operator!(). |
| 854 } else { | 844 } else { |
| 855 ADD_FAILURE() << "Null pointer should result in !x being true."; | 845 ADD_FAILURE() << "Null pointer should result in !x being true."; |
| 856 } | 846 } |
| 857 } | 847 } |
| OLD | NEW |