• cocos3 内存管理机制


    内存管理,防止内存泄露

    如果a类引用某个那么内存块加1,如果a类不在引用了,内存块减一,当计数为0的时候,清空这个内存块。

    retain()函数

    void Ref::retain()
    {
        CCASSERT(_referenceCount > 0, "reference count should greater than 0");
        ++_referenceCount;
    }

    release()函数

    void Ref::release()
    {
        CCASSERT(_referenceCount > 0, "reference count should greater than 0");
        --_referenceCount;
    
        if (_referenceCount == 0)
        {
    #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
            auto poolManager = PoolManager::getInstance();
            if (!poolManager->getCurrentPool()->isClearing() && poolManager->isObjectInPools(this))
            {
                // Trigger an assert if the reference count is 0 but the Ref is still in autorelease pool.
                // This happens when 'autorelease/release' were not used in pairs with 'new/retain'.
                //
                // Wrong usage (1):
                //
                // auto obj = Node::create();   // Ref = 1, but it's an autorelease Ref which means it was in the autorelease pool.
                // obj->autorelease();   // Wrong: If you wish to invoke autorelease several times, you should retain `obj` first.
                //
                // Wrong usage (2):
                //
                // auto obj = Node::create();
                // obj->release();   // Wrong: obj is an autorelease Ref, it will be released when clearing current pool.
                //
                // Correct usage (1):
                //
                // auto obj = Node::create();
                //                     |-   new Node();     // `new` is the pair of the `autorelease` of next line
                //                     |-   autorelease();  // The pair of `new Node`.
                //
                // obj->retain();
                // obj->autorelease();  // This `autorelease` is the pair of `retain` of previous line.
                //
                // Correct usage (2):
                //
                // auto obj = Node::create();
                // obj->retain();
                // obj->release();   // This `release` is the pair of `retain` of previous line.
                CCASSERT(false, "The reference shouldn't be 0 because it is still in autorelease pool.");
            }
    #endif
    
    #if CC_REF_LEAK_DETECTION
            untrackRef(this);
    #endif
            delete this;
        }
    }

    autorelease()相当于 自动的retain()和release()。采用了池的计数

    object的定义

    就是ref

    还可以通过右键点击某个函数,计算层次结构和调用。

  • 相关阅读:
    BZOJ 3992: [SDOI2015]序列统计
    BZOJ 4836: [Lydsy1704月赛]二元运算
    2.Add Two Numbers
    [RN] React Native 调试技巧
    [商业世界] 商业三流:信息流、资金流、物流
    [杂谈] 记 程序员 对抗失眠烦恼的 大法
    [未来成长]让写作成为一种生活习惯
    [未来成长] 分享:《麦肯锡教我的写作武器》如何写出一篇具有逻辑表现力的文案
    [未来成长] 分享:在腾讯的八年,我的职业思考
    [PHP]:AES对称加密 -- 支持PHP7
  • 原文地址:https://www.cnblogs.com/yufenghou/p/4137155.html
Copyright © 2020-2023  润新知