Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CP_AutoReleasePool Class Reference

A class to manage automatic releasing of references to refounted objects. More...

List of all members.

Adding objects

Add the given object reference to the topmost autorelease pool.

void AddObject (const CP_M_RefCounted *inObj)
 Add the given object reference to the this autorelease pool.
static void AddObjectToTopMostPool (const CP_M_RefCounted *inObj)

Public Member Functions

void ReleaseAll ()
 Release all references inside this pool.
Constructor / Destructor
 CP_AutoReleasePool ()
 Constructor.
 ~CP_AutoReleasePool ()
 Destructor.

Protected Attributes

std::vector< const CP_M_RefCounted * > fObjects
 Object references managed by this pool.


Detailed Description

One common problem when using refcounted objects is that functions that create and return pointers to refcounted objects implicitely create a reference to the object by creating it. It's therefore mostly up to the caller of the function to ensure that this implicit reference will be released eventually.
In order to move the responsibility of releasing the reference to the place where it's created, the auto-release pool concept comes in place: at the point of creation, the object reference can be added to an auto-release pool and thus is from now on managed by that auto-release pool. Eventually, the auto-release pool will release all the stored references.
The calling function now gets an object pointer and is only responsible to create additional references if it wants to hold on to the object for longer than the scope of the function (actually, longer than the topmost autorelease pool exists, which, in all but the most arkane situations, is at least equivalent to the scope of the function).
Typical usage of autorelease pools is by adding the object to the topmost autorelease pool after creating it:
 A* getAnA()
 {
	 A* a = new A;
	 a->AutoRelease();
	 return a;
 }
 
 void foo()
 {
	 A* a = getAnA(); // no idea whether foo creates A, but it doesn't matter, since autorelease pool takes care.
	 
	 // ... do something with a
	 
	 // no need to release a here.
 }
 
Note:
Of course, using autorelease pool will extend the life time of the refcounted object beyond the controlled function scope. This is not by itself a problem, but one should be aware of this fact.


Constructor & Destructor Documentation

CP_AutoReleasePool::CP_AutoReleasePool  ) 
 

Create a new autorelease pool and push it onto the stack of available pools. Any subsequent call to AddObjectToTopMostPool will result in the object to be added to the new autorelease pool.

CP_AutoReleasePool::~CP_AutoReleasePool  ) 
 


Member Function Documentation

void CP_AutoReleasePool::AddObjectToTopMostPool const CP_M_RefCounted inObj  )  [static]
 

Parameters:
inObj A pointer to a reference counted object.

void CP_AutoReleasePool::AddObject const CP_M_RefCounted inObj  ) 
 

Parameters:
inObj A pointer to a reference counted object.

void CP_AutoReleasePool::ReleaseAll  ) 
 

Releases all registered object references in reverse order of addition.


Member Data Documentation

std::vector<const CP_M_RefCounted*> CP_AutoReleasePool::fObjects [protected]
 


The documentation for this class was generated from the following files:
Generated on Sat Sep 17 20:01:22 2005 for CPLAT by  doxygen 1.4.0