zoukankan      html  css  js  c++  java
  • 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

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

  • 相关阅读:
    在maven工程中使用groovy
    groovy学习1搭建环境
    Android 中运行时权限获取联系人信息 Demo
    Android 拍照或相册选择照片进行显示缩放位图 Demo
    Android 热点相关操作
    Android 内嵌 HTML5 并进行交互
    AJAX实现局部刷新
    C#主要用于查询sql的web项目:查询以及页面显示数据非常缓慢的改进方案
    配置和读取web.config数据库
    web项目在服务器IIS7上的部署:达到内部网可以通过输入网页直接访问的效果
  • 原文地址:https://www.cnblogs.com/yufenghou/p/4137155.html
Copyright © 2011-2022 走看看