Back to index
RefCounted<T> is a sort of a "mixin" for a class T. More...
|int||refCount () const|
|bool||hasOneRef () const|
|class||RefPtr< T >|
RefCounted<T> is a sort of a "mixin" for a class T.
RefCounted manages, well, refcounting for T, and because RefCounted is parameterized on T, RefCounted<T> can call T's destructor directly. This means T doesn't need to have a virtual dtor and so doesn't need a vtable.
RefCounted<T> is created with refcount == 0. Newly-allocated RefCounted<T> must immediately be assigned to a RefPtr to make the refcount > 0. It's an error to allocate and free a bare RefCounted<T>, i.e. outside of the RefPtr machinery. Attempts to do so will abort DEBUG builds.
Live RefCounted<T> have refcount > 0. The lifetime (refcounts) of live RefCounted<T> are controlled by RefPtr<T> and RefPtr<super/subclass of T>. Upon a transition from refcounted==1 to 0, the RefCounted<T> "dies" and is destroyed. The "destroyed" state is represented in DEBUG builds by refcount==-0xdead. This state distinguishes use-before-ref (refcount==0) from use-after-destroy (refcount==-0xdead).