-
Notifications
You must be signed in to change notification settings - Fork 421
CAObject
CAObject, root class of CrossApp engine, is mainly used to manage memory and define some call-back function pointers. The most classes of engine derive from it, and they all follow the same memory management mode.
Access modifier |
Attribute name |
Description |
Public |
m_uID |
The only id of object |
Protected |
m_uReference |
Reference counter |
Protected |
m_uAutoReleaseCount |
Auto release reference counter |
Access modifier |
Method name |
Description |
Public |
retain |
Add object counter |
Public |
release |
Minus object counter |
Public |
autorelease |
Set auto management mode |
Public |
retainCount |
Return current counter value of object |
Public |
isSingleReference |
Instantiation object only has one user or not |
Public |
isEqual |
Judge current object is same with appointed CAObject instantiation object or not |
m_uID
Type: unsigned int
Description: It deploys accumulation mode and has a 0 initial value. Every time we create an object, m_u ID adds 1 as well as initialize current object’s m_uReference as 1 and m_uAutoReleaseCount as 0.
Example:
CAObject::CAObject(
void
): m_uReference(1) , m_uAutoReleaseCount(0)
{
static
unsigned
int
uObjectCount = 0;
m_uID = ++uObjectCount;
}
m_uReference
Type: unsigned int
Description: reference counter, counter’s value is auto added 1 when each new object is created.
m_uAutoReleaseCount
Type: unsigned int
Description: auto manage object or not, if m_uAutoReleaseCount’s value is 0 then it deploys non-auto manage mode, when m_uAutoReleaseCount’s value is not 0 then it deploys auto manage mode.
void retain(void)
Return value: void
Description: add 1 on m_uReference value of reference counter
Example:
void
CAObject::retain(
void
)
{
++m_uReference;
}
void release(void)
Return value: void
Description: minus 1 on m_uReference value of reference counter, if m_uReference value is 0 then delete this object.
Example:
void
CAObject::release(
void
)
{
--m_uReference;
if
(m_uReference == 0)
{
delete
this
;
}
}
CAObject autorelease(void)*
Return value: CAObject*
Description: set current object as auto memory management mode
Example:
CAObject* CAObject::autorelease(
void
)
{
CCPoolManager::sharedPoolManager()->addObject(
this
);
return
this
;
}
unsigned int retainCount(void) const
Return value: unsigned int
Description: return m_uReference value of current reference counter
Example:
unsigned
int
CAObject::retainCount(
void
)
const
{
return
m_uReference;
}
bool isSingleReference(void) const
Return value: bool
Description: return if object only has one user or not via comparing to reference counter value
Example:
bool
CAObject::isSingleReference(
void
)
const
{
return
m_uReference == 1;
}
virtual bool isEqual(const CAObject pObject)*
Return value: bool
Description: virtual function, judge current object is same with instantiation object or not
Example:
bool
CAObject::isEqual(
const
CAObject *pObject)
{
return
this
== pObject;
}
CrossApp conducts memory management by adopting reference counter mode and all CAObject derived classes use this mode to manage object. The most basic rule is that the one who is in charge of new/retain also is responsible of release. When we create an object system will auto add a counter, and it will run a plus 1 operation on counter via retain method for every object citing; on the contrary, system will run a minus 1 operation on counter by calling release method for every object citing release. If reference counter’s m_uReference value is 0, then this object will be deleted.
Besides, CrossApp also provides another auto memory management method: when we are not sure about object’s release timing, we can call autorelease method to defer object release operation, at this moment object is in auto management status and is added to auto release pool. System will auto release all objects in pool to run a release operation after every time a frame is finished. If m_uReference value is 0, then current object will be deleted.