Public Member Functions | |
TMCCountedPtr () | |
TMCCountedPtr (T *lp) | |
TMCCountedPtr (const TMCCountedPtr< T > &lp) | |
~TMCCountedPtr () | |
uint32 | Release () |
operator T * () const | |
T & | operator* () const |
T ** | operator& () |
T *const * | operator& () const |
TMCCountedPtr< T > * | GetAddress () |
T * | operator-> () const |
T * | operator= (T *lp) |
T * | operator= (const TMCCountedPtr< T > &lp) |
boolean | operator== (const TMCCountedPtr< T > &rhs) const |
boolean | operator== (const T *rhs) const |
boolean | operator== (T *rhs) const |
BOOL | operator! () |
void | CheckLastRef (void) |
Static Public Member Functions | |
static T * | Assign (T **pp, T *lp) |
Public Attributes | |
T * | fObject |
Counted objects are objects that use a counter to keep track of how many other objects need them, and delete themselves when nobody needs them anymore. A counted object has an AddRef method that increments its counter, and a Release method that decrements it. When the value of the counter reaches zero, the object is deleted.
It is possible to call AddRef and Release "by hand" but it is a bad idea for at least two reasons. First, it is very easy to forget to add a call to Release, therefore causing a memory leak. Second the code becomes more complex because in every function that calls AddRef, you must consider exceptions. So you have to use a try/catch block, add a call to Release at the end of the try AND another one before re-throwing in the catch section...
Because they automatically call AddRef and Release, TMCCountedPtr, TMCCountedGetHelper and TMCCountedCreateHelper are your best friends.
TMCCountedPtr overrides many operators (including *, ->, &, =, and cast) so that you can use it like other pointers (the difference being the calls to AddRef and Release when you assign the pointer). Here is a pseudo code sample:
void CountedPointersSample() { TMCCountedPtr<IMCFile> file; //a counted pointer to a file object (initialized to NULL) gFileUtilities->CreateIMCFile(&file); //ask the application to create a file (its refcount will be 1) IMCFile* file2= pfile; //that's legal (but doesn't addref) if (MCVerify(file)) //just to illustrate a test with an assert { TMCCountedPtr<IMCFile> file3; file3= file2; //the refcount becomes 2 because file3 is a counted pointer } //the refcount becomes 1 as the destructor of file3 is called file = NULL; //the refcount becomes 0 and the file object is deleted. //at this point file2 is invalid. Lets set it to null so nobody corrupts the memory by trying to use it. file2= NULL; }
For more information, please refer to the "Counted Pointers" chapter in the overview of the SDK.
TMCCountedPtr< T >::TMCCountedPtr | ( | ) | [inline] |
TMCCountedPtr< T >::TMCCountedPtr | ( | T * | lp | ) | [inline, explicit] |
TMCCountedPtr< T >::TMCCountedPtr | ( | const TMCCountedPtr< T > & | lp | ) | [inline] |
TMCCountedPtr< T >::~TMCCountedPtr | ( | ) | [inline] |
static T* TMCCountedPtr< T >::Assign | ( | T ** | pp, | |
T * | lp | |||
) | [inline, static] |
void TMCCountedPtr< T >::CheckLastRef | ( | void | ) | [inline] |
TMCCountedPtr<T>* TMCCountedPtr< T >::GetAddress | ( | ) | [inline] |
TMCCountedPtr< T >::operator T * | ( | ) | const [inline] |
BOOL TMCCountedPtr< T >::operator! | ( | ) | [inline] |
T* const* TMCCountedPtr< T >::operator& | ( | ) | const [inline] |
T** TMCCountedPtr< T >::operator& | ( | ) | [inline] |
T& TMCCountedPtr< T >::operator* | ( | ) | const [inline] |
T* TMCCountedPtr< T >::operator-> | ( | ) | const [inline] |
T* TMCCountedPtr< T >::operator= | ( | const TMCCountedPtr< T > & | lp | ) | [inline] |
Reimplemented in TMCListenedLayer, and TMCListened< T >.
T* TMCCountedPtr< T >::operator= | ( | T * | lp | ) | [inline] |
Reimplemented in TMCListenedLayer, and TMCListened< T >.
boolean TMCCountedPtr< T >::operator== | ( | T * | rhs | ) | const [inline] |
boolean TMCCountedPtr< T >::operator== | ( | const T * | rhs | ) | const [inline] |
boolean TMCCountedPtr< T >::operator== | ( | const TMCCountedPtr< T > & | rhs | ) | const [inline] |
uint32 TMCCountedPtr< T >::Release | ( | ) | [inline] |
Reimplemented in TMCListenedLayer, and TMCListened< T >.
T* TMCCountedPtr< T >::fObject |