template<class T, typename Traits = DefaultRefCountedTraits<T>>
class base::RefCounted< T, Traits >
A base class for reference counted classes.
Otherwise, known as a cheap knock-off of WebKit's RefCounted<T> class. To use this, just extend your class from it like so:
class MyFoo : public base::RefCounted<MyFoo> {
...
private:
friend class base::RefCounted<MyFoo>;
~MyFoo();
};
Usage Notes:
- You should always make your destructor non-public, to avoid any code deleting the object accidentally while there are references to it.
- You should always make the ref-counted base class a friend of your class, so that it can access the destructor.
The ref count manipulation to RefCounted is NOT thread safe and has DCHECKs to trap unsafe cross thread usage. A subclass instance of RefCounted can be passed to another execution thread only when its ref count is 1. If the ref count is more than 1, the RefCounted class verifies the ref updates are made on the same execution thread as the previous ones. The subclass can also manually call IsOnValidThread to trap other non-thread-safe accesses; see the documentation for that method.