nsCountedRef<T> is a template class implementing an object that takes a strong reference to a reference-counted resource that must be released, typically on destruction of the object.

nsCountedRef has a role similar to nsRefPtr but does not require that the handle is a pointer to an object that has AddRef() and Release() methods. nsCountedRef is therefore useful for managing references to foreign objects.

nsCountedRef<T> is derived from nsAutoRef<T> and so inherits its methods. nsCountedRef differs from nsAutoRef in that nsCountedRef constructors add a reference to the resource, and in that nsCountedRef provides copy construction< and assignment operators enabling more than one concurrent reference to the same resource.

In order to use nsCountedRef<T> for a class T associated with a particular resource type, the type of the handle to the resource and the methods for referencing and releasing the resource must be provided for class T.  This is usually done by providing a specialization of nsAutoRefTraits<T>.

nsCountedRef<T> is defined in xpcom/base/nsAutoRef.h.

See Also

nsAutoRef, nsAutoRefTraits