Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: base/memory/scoped_ptr.h

Issue 1356483002: Call reset(nullptr) in scoped_ptr's. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and change to reset(). Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/memory/scoped_ptr_unittest.cc » ('j') | base/memory/scoped_ptr_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 if (data_.ptr != nullptr) { 219 // Match libc++, which calls reset() in its destructor.
220 // Not using get_deleter() saves one function call in non-optimized 220 // Use nullptr as the new value for three reasons:
221 // builds. 221 // 1. libc++ does it.
222 static_cast<D&>(data_)(data_.ptr); 222 // 2. Avoids infinitely recursing into destructors if two classes are owned
223 } 223 // in a reference cycle (see ScopedPtrTest.ReferenceCycle).
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);
224 } 230 }
225 231
226 void reset(T* p) { 232 void reset(T* p) {
227 // This is a self-reset, which is no longer allowed for default deleters: 233 // This is a self-reset, which is no longer allowed for default deleters:
228 // https://crbug.com/162971 234 // https://crbug.com/162971
229 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr); 235 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr);
230 236
231 // Match C++11's definition of unique_ptr::reset(), which requires changing 237 // Match C++11's definition of unique_ptr::reset(), which requires changing
232 // the pointer before invoking the deleter on the old pointer. This prevents 238 // the pointer before invoking the deleter on the old pointer. This prevents
233 // |this| from being accessed after the deleter is run, which may destroy 239 // |this| from being accessed after the deleter is run, which may destroy
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 scoped_ptr<T> make_scoped_ptr(T* ptr) { 578 scoped_ptr<T> make_scoped_ptr(T* ptr) {
573 return scoped_ptr<T>(ptr); 579 return scoped_ptr<T>(ptr);
574 } 580 }
575 581
576 template <typename T> 582 template <typename T>
577 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { 583 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) {
578 return out << p.get(); 584 return out << p.get();
579 } 585 }
580 586
581 #endif // BASE_MEMORY_SCOPED_PTR_H_ 587 #endif // BASE_MEMORY_SCOPED_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/scoped_ptr_unittest.cc » ('j') | base/memory/scoped_ptr_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698