1*6777b538SAndroid Build Coastguard Worker# //base/memory Types 2*6777b538SAndroid Build Coastguard Worker 3*6777b538SAndroid Build Coastguard Worker## Overview 4*6777b538SAndroid Build Coastguard WorkerThis directory contains a variety of pointer-like objects (aka smart pointers). 5*6777b538SAndroid Build Coastguard WorkerThis is a brief overview of what they are and how they should be used. Refer to 6*6777b538SAndroid Build Coastguard Workerindividual header files for details. C++ is not memory safe, so use these types 7*6777b538SAndroid Build Coastguard Workerto help guard against potential memory bugs. 8*6777b538SAndroid Build Coastguard WorkerThere are other pointer-like object types implemented elsewhere that may be 9*6777b538SAndroid Build Coastguard Workerright for a given use case, such as `absl::optional<T>` and 10*6777b538SAndroid Build Coastguard Worker`std::unique_ptr<T>`. More on all types in video form 11*6777b538SAndroid Build Coastguard Worker[here](https://youtu.be/MpwbWSEDfjM?t=582s) and in a doc 12*6777b538SAndroid Build Coastguard Worker[here](https://docs.google.com/document/d/1VRevv8JhlP4I8fIlvf87IrW2IRjE0PbkSfIcI6-UbJo/edit?usp=sharing). 13*6777b538SAndroid Build Coastguard Worker 14*6777b538SAndroid Build Coastguard Worker## `raw_ptr<T>` 15*6777b538SAndroid Build Coastguard WorkerUse for class fields/members that would otherwise be a `T*`. 16*6777b538SAndroid Build Coastguard Worker 17*6777b538SAndroid Build Coastguard WorkerThis is a weakly refcounted wrapper for a `T*` (also called a raw 18*6777b538SAndroid Build Coastguard Workerpointer). When the object is deleted, the allocator will "poison" the memory 19*6777b538SAndroid Build Coastguard Workerthat object occupied and keep the memory around so it’s not reused. This reduces 20*6777b538SAndroid Build Coastguard Workerthe risk and impact of a use-after-free bug. 21*6777b538SAndroid Build Coastguard Worker 22*6777b538SAndroid Build Coastguard WorkerDepending on the use case, it's possible a smart pointer with additional 23*6777b538SAndroid Build Coastguard Workerfeatures would be more appropriate, but if none of those are applicable or 24*6777b538SAndroid Build Coastguard Workernecessary, `raw_ptr<T>` is preferred over a `T*`. 25*6777b538SAndroid Build Coastguard Worker 26*6777b538SAndroid Build Coastguard WorkerFor more information, see [`raw_ptr.md`](./raw_ptr.md); for guidance on 27*6777b538SAndroid Build Coastguard Workerusage, see 28*6777b538SAndroid Build Coastguard Worker[the style guide](../../styleguide/c++/c++.md#non_owning-pointers-in-class-fields). 29*6777b538SAndroid Build Coastguard Worker 30*6777b538SAndroid Build Coastguard Worker## `raw_ref<T>` 31*6777b538SAndroid Build Coastguard WorkerUse for class fields/members that would otherwise be a `T&`. 32*6777b538SAndroid Build Coastguard Worker 33*6777b538SAndroid Build Coastguard WorkerThis shares much in common with `raw_ptr<T>`, but asserts that the 34*6777b538SAndroid Build Coastguard Worker`raw_ref<T>` is not nullable. 35*6777b538SAndroid Build Coastguard Worker 36*6777b538SAndroid Build Coastguard WorkerFor more information, see [`raw_ptr.md`](./raw_ptr.md); for guidance on 37*6777b538SAndroid Build Coastguard Workerusage, see 38*6777b538SAndroid Build Coastguard Worker[the style guide](../../styleguide/c++/c++.md#non_owning-pointers-in-class-fields). 39*6777b538SAndroid Build Coastguard Worker 40*6777b538SAndroid Build Coastguard Worker## `base::WeakPtr<T>` 41*6777b538SAndroid Build Coastguard WorkerUse when a reference to an object might outlive the object itself. 42*6777b538SAndroid Build Coastguard Worker 43*6777b538SAndroid Build Coastguard WorkerThese are useful for asynchronous work, which is common in Chrome. If an async 44*6777b538SAndroid Build Coastguard Workertask references other objects or state, and it's possible for that state to be 45*6777b538SAndroid Build Coastguard Workerdestroyed before the task runs, those references should be held in a 46*6777b538SAndroid Build Coastguard Worker`WeakPtr<T>`. Each `WeakPtr<T>` is associated with a `WeakPtrFactory<T>`. When 47*6777b538SAndroid Build Coastguard Workerthe associated factory (usually owned by T) is destroyed, all `WeakPtr<T>` are 48*6777b538SAndroid Build Coastguard Workerinvalidated (becomes null) rather than becoming use-after-frees. If such 49*6777b538SAndroid Build Coastguard Workerreferences should never outlive the object, consider using SafeRef instead. 50*6777b538SAndroid Build Coastguard Worker 51*6777b538SAndroid Build Coastguard Worker## `base::SafeRef<T>` 52*6777b538SAndroid Build Coastguard WorkerUse to express that a reference to an object must not outlive the object. 53*6777b538SAndroid Build Coastguard Worker 54*6777b538SAndroid Build Coastguard WorkerAn example is if you have a class member that you want to guarantee outlives the 55*6777b538SAndroid Build Coastguard Workerclass itself. SafeRef automatically enforces the lifetime assumptions and 56*6777b538SAndroid Build Coastguard Workereliminates the need for validity checks. 57*6777b538SAndroid Build Coastguard Worker 58*6777b538SAndroid Build Coastguard WorkerIf the assumption that the object is valid is broken, then the process 59*6777b538SAndroid Build Coastguard Workerterminates safely and generates a crash report. Though not ideal, it's 60*6777b538SAndroid Build Coastguard Workerpreferable to a potentially undiscovered security bug. 61*6777b538SAndroid Build Coastguard Worker 62*6777b538SAndroid Build Coastguard WorkerThis type is built on top of WeakPtr, so if you want a `SafeRef<T>`, T needs a 63*6777b538SAndroid Build Coastguard WorkerWeakPtrFactory as a member. It works like `WeakPtr`, but doesn't allow for a 64*6777b538SAndroid Build Coastguard Workernull state. There's also overlap with `raw_ptr`, though this was implemented 65*6777b538SAndroid Build Coastguard Workerfirst. 66*6777b538SAndroid Build Coastguard Worker 67*6777b538SAndroid Build Coastguard Worker## `scoped_refptr<T>` 68*6777b538SAndroid Build Coastguard WorkerUse when you want manually managed strong refcounting. Use carefully! 69*6777b538SAndroid Build Coastguard Worker 70*6777b538SAndroid Build Coastguard WorkerIt’s an owning smart pointer, so it owns a pointer to something allocated in the 71*6777b538SAndroid Build Coastguard Workerheap and gives shared ownership of the underlying object, since it can be 72*6777b538SAndroid Build Coastguard Workercopied. When all `scoped_refptr<T>`s pointing to the same object are gone, that 73*6777b538SAndroid Build Coastguard Workerobject gets destroyed. 74*6777b538SAndroid Build Coastguard Worker 75*6777b538SAndroid Build Coastguard WorkerThis is Chrome's answer to `std::shared_ptr<T>`. It additionally requires T to 76*6777b538SAndroid Build Coastguard Workerinherit from `RefCounted` or `RefCountedThreadSafe`, since the ref counting 77*6777b538SAndroid Build Coastguard Workerhappens in the object itself, unlike `shared_ptr<T>`. 78*6777b538SAndroid Build Coastguard Worker 79*6777b538SAndroid Build Coastguard WorkerIt's preferred for an object to remain on the same thread, as `RefCounted` is 80*6777b538SAndroid Build Coastguard Workermuch cheaper. If there are `scoped_refptr<T>`s to the same object on different 81*6777b538SAndroid Build Coastguard Workerthreads, use `RefCountedThreadSafe`, since accesses to the reference count can 82*6777b538SAndroid Build Coastguard Workerrace. In this case, without external synchronization, the destructor of 83*6777b538SAndroid Build Coastguard Worker`scoped_refptr<T>`, which decreases the reference count by one, can run on any 84*6777b538SAndroid Build Coastguard Workerthread. 85*6777b538SAndroid Build Coastguard Worker 86*6777b538SAndroid Build Coastguard WorkerInheriting from `RefCountedThreadSafe` by itself doesn't make a class `T` or the 87*6777b538SAndroid Build Coastguard Workerunderlying object of `scoped_refptr<T>` thread-safe: It merely ensures that the 88*6777b538SAndroid Build Coastguard Workercounter manipulated by `scoped_refptr<T>` is thread-safe. 89*6777b538SAndroid Build Coastguard Worker 90*6777b538SAndroid Build Coastguard WorkerIf the destructor interacts with other systems it is important to 91*6777b538SAndroid Build Coastguard Workercontrol and know which thread has the last reference to the object, or you can 92*6777b538SAndroid Build Coastguard Workerend up with flakiness. 93