The life span of a Core Foundation object is determined by its reference count, or (to avoid confusion with other forms of referencing) its retention count: an internal count of the number of clients who want the object to persist. When you create or copy an object in Core Foundation, its retention count is set to one. Subsequent clients can claim ownership of the object by calling
CFRetain
which increments the retention count. Later, when you have no more use for the object, you call
CFRelease
. When the retention count reaches zero, the object's allocator deallocates the object's memory.
When you copy an object, the resulting object has a retention count of one regardless of the retention count of the original object. For more on copying objects, see Copying Core Foundation Objects.
ImportantYou should never directly deallocate a Core Foundation object (for instance, by callingfree
on it.) When you are finished with an object, call theCFRelease
function and Core Foundation will properly dispose of it.