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学习笔记(19)--label 、label atlas
  • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
  • Cocos2d-x学习(一):HelloWorld
  • cocos2dx在xcode下开发,编译到android上(2)
  • cocos2d 设置屏幕默认方向
  • Cocos2d-x 2.0 之 Actions “三板斧” 之一
  • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
  • cocos2d-x学习笔记(18)--游戏打包(windows平台)
  • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1