Cocos2d-x 3.0 Memory Management Mechanism

Source: Internet
Author: User

Cocos2d-x 3.0 Memory Management Mechanism Analysis entry

* *********************************** Please specify the source: bytes ********************************************





Again, the memory management mechanism

1. Concise Mechanism

2. Code view Mechanism




1. Brief introduction-Memory Management Mechanism of Cocos2d-x

Speaking of memory management, this is Question, (⊙ v ⊙ ..

Let's take a look at how memory management is implemented in various languages?

-- Java:

In the heap area, memory is allocated by new and recycled by the garbage collector.

(Specific complex, you can look at this article-> http://blog.csdn.net/tutngfei1129287460/article/details/7383480)

-- C ++:

New to create, delete to delete

-- Objective-C:

Reference COUNTING METHOD

Create an obj. Each OBJ has a retaincount,

When new, alloc, and retain are used, this retaincount + 1;

If you perform the release operation on this object, the retainCount-1 is made.

Therefore, each object has two methods: retain and release.

When retaincount is 0, this object is released.

The Cocos2d-x uses C ++ to realize the management mechanism of objective-C.





2. Code view-Memory Management Mechanism of cocos2d-x

Create an object. Let's take a look at this management mechanism.

Open the project, and you can see in the Entry Program appdelegate. cpp:

 // create a scene. it's an autorelease objectauto scene = HelloWorld::createScene();    // run    director->runWithScene(scene);

Create a scenario, run it, track it in, and create a function for the scenario:

// 'scene' is an autorelease object    auto scene = Scene::create();        // 'layer' is an autorelease object    auto layer = HelloWorld::create();

The createscene function creates scenes and layers.

The create method is used. Both scene and layer belong to the node subclass, and node is also the ref subclass.

As mentioned before, the ref class is the memory management mechanism of the cocos2d-x.

The ref class can be found as follows:

<span style="font-size:14px;">class CC_DLL Ref{public:    /**     * Retains the ownership.     *     * This increases the Ref's reference count.     *     * @see release, autorelease     * @js NA     */    void retain();        /**     * Release the ownership immediately.     *     * This decrements the Ref's reference count.     *     * If the reference count reaches 0 after the descrement, this Ref is     * destructed.     *     * @see retain, autorelease     * @js NA     */    void release();    /**     * Release the ownership sometime soon automatically.     *     * This descrements the Ref's reference count at the end of current     * autorelease pool block.     *     * If the reference count reaches 0 after the descrement, this Ref is     * destructed.     *     * @returns The Ref itself.     *     * @see AutoreleasePool, retain, release     * @js NA     * @lua NA     */    Ref* autorelease();    /**     * Returns the Ref's current reference count.     *     * @returns The Ref's reference count.     * @js NA     */    unsigned int getReferenceCount() const;    protected:    /**     * Constructor     *     * The Ref's reference count is 1 after construction.     * @js NA     */    Ref();    public:    /**     * @js NA     * @lua NA     */    virtual ~Ref();    protected:    /// count of references    unsigned int _referenceCount;        friend class AutoreleasePool;    #if CC_ENABLE_SCRIPT_BINDINGpublic:    /// object id, ScriptSupport need public _ID    unsigned int        _ID;    /// Lua reference id    int                 _luaID;#endif};</span>

..... Long

There are:

Retain method (add reference ),

Release Method (reduce references ),

Autorelease method (for automatic release)

Getreferencecount (retrieve reference count)

Ref (),~ Ref () constructor and destructor

_ Referencecount: This is the reference value.

Autoreleasepaul youyuan class

_ Id, _ luaid support for JS and Lua scripts


Let's take a look at the specific definition of this class,

You can find the ref class in the project folder cocos2d/Cocos/base /.

You can also find the ref class in the external dependency item after vs2012 is opened, but all files are. h header files. We want to see. cpp yo

However, when this class is found, it is called ccref

-- In, the ref constructor can see:

Ref::Ref(): _referenceCount(1) // when the Ref is created, the reference count of it is 1
That is to say, when an object is created, its reference value is initialized to 1.

-- When the retain method is executed

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

-- When the release method is executed

void Ref::release(){    CCASSERT(_referenceCount > 0, "reference count should greater than 0");    --_referenceCount;
Will reference value --

-- And, after release, determine whether the value is 0. If the value is 0, delete this:

    if (_referenceCount == 0)    {#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)        auto poolManager = PoolManager::getInstance();        if (!poolManager->getCurrentPool()->isClearing() && poolManager->isObjectInPools(this))        {             CCASSERT(false, "The reference shouldn't be 0 because it is still in autorelease pool.");        }#endif        delete this;    }

From this point of view, the cocos2d-x controls the lifecycle of an object by referencing the count. Therefore, in cocos2d-x programming, there is basically no need for the delete statement, only the non-ref base class or the ref class will use the delete statement.



Let's look back at the create method of scene:

Scene *Scene::create(){    Scene *ret = new Scene();    if (ret && ret->init())    {        ret->autorelease();        return ret;    }    else    {        CC_SAFE_DELETE(ret);        return nullptr;    }}
Here, we first create a new scenario, then execute the scenario initialization method (init), and then add the current object to the Auto Release pool for management,

This method is defined as follows:

Ref* Ref::autorelease(){    PoolManager::getInstance()->getCurrentPool()->addObject(this);    return this;}
In this way, we do not need to encode the object explicitly. When the object is referenced, the release method is automatically implemented.


-- End


Well, here we are. I wanted to run it with an instance, but some problems may occur.

Tomorrow is about to go. It may take some time ..




References:

Sdhjob (Instructor Shen)






* *********************************** Please specify the source: bytes ********************************************

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.