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 // Scopers help you manage ownership of a pointer, helping you easily manage a | 5 // Scopers help you manage ownership of a pointer, helping you easily manage a |
6 // pointer within a scope, and automatically destroying the pointer at the end | 6 // pointer within a scope, and automatically destroying the pointer at the end |
7 // of a scope. There are two main classes you will use, which correspond to the | 7 // of a scope. There are two main classes you will use, which correspond to the |
8 // operators new/delete and new[]/delete[]. | 8 // operators new/delete and new[]/delete[]. |
9 // | 9 // |
10 // Example usage (scoped_ptr<T>): | 10 // Example usage (scoped_ptr<T>): |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 | 209 |
210 template <typename U, typename V> | 210 template <typename U, typename V> |
211 void TakeState(scoped_ptr_impl<U, V>* other) { | 211 void TakeState(scoped_ptr_impl<U, V>* other) { |
212 // See comment in templated constructor above regarding lack of support | 212 // See comment in templated constructor above regarding lack of support |
213 // for move-only deleters. | 213 // for move-only deleters. |
214 reset(other->release()); | 214 reset(other->release()); |
215 get_deleter() = other->get_deleter(); | 215 get_deleter() = other->get_deleter(); |
216 } | 216 } |
217 | 217 |
218 ~scoped_ptr_impl() { | 218 ~scoped_ptr_impl() { |
219 // Match libc++, which calls reset() in its destructor. | 219 if (data_.ptr != nullptr) { |
220 // Use nullptr as the new value for three reasons: | 220 // Not using get_deleter() saves one function call in non-optimized |
221 // 1. libc++ does it. | 221 // builds. |
222 // 2. Avoids infinitely recursing into destructors if two classes are owned | 222 static_cast<D&>(data_)(data_.ptr); |
223 // in a reference cycle (see ScopedPtrTest.ReferenceCycle). | 223 } |
224 // 3. If |this| is accessed in the future, in a use-after-free bug, attempts | |
225 // to dereference |this|'s pointer should cause either a failure or a | |
226 // segfault closer to the problem. If |this| wasn't reset to nullptr, | |
227 // the access would cause the deleted memory to be read or written | |
228 // leading to other more subtle issues. | |
229 reset(nullptr); | |
230 } | 224 } |
231 | 225 |
232 void reset(T* p) { | 226 void reset(T* p) { |
233 // This is a self-reset, which is no longer allowed for default deleters: | 227 // This is a self-reset, which is no longer allowed for default deleters: |
234 // https://crbug.com/162971 | 228 // https://crbug.com/162971 |
235 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr); | 229 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr); |
236 | 230 |
237 // Match C++11's definition of unique_ptr::reset(), which requires changing | 231 // Match C++11's definition of unique_ptr::reset(), which requires changing |
238 // the pointer before invoking the deleter on the old pointer. This prevents | 232 // the pointer before invoking the deleter on the old pointer. This prevents |
239 // |this| from being accessed after the deleter is run, which may destroy | 233 // |this| from being accessed after the deleter is run, which may destroy |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 572 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
579 return scoped_ptr<T>(ptr); | 573 return scoped_ptr<T>(ptr); |
580 } | 574 } |
581 | 575 |
582 template <typename T> | 576 template <typename T> |
583 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { | 577 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { |
584 return out << p.get(); | 578 return out << p.get(); |
585 } | 579 } |
586 | 580 |
587 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 581 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |