cocos2dx 之内存管理
来源:未知 责任编辑:责任编辑 发表时间:2014-01-20 07:51 点击:次
cocos2dx 采用引用计数管理自己的内存:
引用计数:
引用计数就是通过给每个对象维护一个引用计数器,记录该对象当前被引用的次数。当对象增加一次引用时,计数器+1.而对象失去一次引用时,计数器-1,当引用计数器为0时。标志着该对象的生命周期结束,自动触发对象的回收释放。 为了实现引用计数器,cocos2dx实现了自己的根类ccobject。引 擎中所有类都派生自ccobject类。一下ccobject的定义class CC_DLL Object
{
public:
/// object id, ScriptSupport need public _ID
unsigned int _ID;
/// Lua reference id
int _luaID;
protected:
/// count of references
unsigned int _reference;
/// count of autorelease
unsigned int _autoReleaseCount;
public:
/**
* Constructor
*
* The object's reference count is 1 after construction.
* @js NA
*/
Object();
/**
* @js NA
* @lua NA
*/
virtual ~Object();
/**
* Release the ownership immediately.
*
* This decrements the object's reference count.
*
* If the reference count reaches 0 after the descrement, this object is
* destructed.
*
* @see retain, autorelease
* @js NA
*/
inline void release()
{
CCASSERT(_reference > 0, "reference count should greater than 0");
--_reference;
if (_reference == 0)
delete this;
}
/**
* Retains the ownership.
*
* This increases the object's reference count.
*
* @see release, autorelease
* @js NA
*/
inline void retain()
{
CCASSERT(_reference > 0, "reference count should greater than 0");
++_reference;
}
/**
* Release the ownership sometime soon automatically.
*
* This descrements the object's reference count at the end of current
* autorelease pool block.
*
* If the reference count reaches 0 after the descrement, this object is
* destructed.
*
* @returns The object itself.
*
* @see AutoreleasePool, retain, release
* @js NA
* @lua NA
*/
Object* autorelease();
/**
* Returns a boolean value that indicates whether there is only one
* reference to the object. That is, whether the reference count is 1.
*
* @returns Whether the object's reference count is 1.
* @js NA
*/
bool isSingleReference() const;
/**
* Returns the object's current reference count.
*
* @returns The object's reference count.
* @js NA
*/
unsigned int retainCount() const;
/**
* Returns a boolean value that indicates whether this object and a given
* object are equal.
*
* @param object The object to be compared to this object.
*
* @returns True if this object and @p object are equal, otherwise false.
* @js NA
* @lua NA
*/
virtual bool isEqual(const Object* object);
/**
* @js NA
* @lua NA
*/
virtual void acceptVisitor(DataVisitor &visitor);
/**
* @js NA
* @lua NA
*/
virtual void update(float dt) {CC_UNUSED_PARAM(dt);};
friend class AutoreleasePool;
};
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 进入详细评论页>>

![cocos2d_x+lua[2]](/uploads/allimg/131030/110J64609-0-lp.jpg)








