Cocos2d-x Source Code Analysis: Ref (ccobject) source code Analysis Cocos2d-x memory Management policy

Source: Internet
Author: User

From the source code version number 3.x. Reprint Please specify

Cocos2d-x Total folder source code Analysis:

http://blog.csdn.net/u011225840/article/details/31743129


1.ref,autoreleasepool. PoolmanagerRef includes a reference count called Referencecount, and when a variable of a ref class is new, its Referencecount reference count is set to 1. There are three important operations, retain.        Release,autorelease, the following source code analysis is specified. The autoreleasepool is stored in ref that is shown to call Autorealse. and call its clear function after each frame.        The call that appears is stored in the Realse function of ref, and then empties itself. Poolmanager management of the desired autoreleasepool, no fault, not only a autoreleasepool, there are multiple autoreleasepool.
2. Source code analysis assume that you want a clear understanding of the Cocos2d-x 3.x memory management mechanism. Please be patient to read the code here, the source itself is clear and understandable, besides I have added some necessary Chinese gaze it ~
2.1 Ref source code analyzes important member variables: _referencecount, which is set to 1 in the constructor, remember.

void Ref::retain () {    Ccassert (_referencecount > 0, "Reference count should greater than 0");    ++_referencecount;}
ref* ref::autorelease () {    poolmanager::getinstance ()->getcurrentpool ()->addobject (this);    return this;}
void Ref::release () {    Ccassert (_referencecount > 0, "Reference count should greater than 0");    --_referencecount;    if (_referencecount = = 0)    {        Delete this;    }}

Some of them debug or trace memory leak macros and functions I have removed, in fact, the essence of these three functions is so simple, retain and release respectively add and reduce referencecount, And the release function deletes itself at count 0 o'clock.         The Autorelease function puts ref into the current autorealsepool. Attention please: There is a ccassert inside all three functions.                                           The referencecout of the object must be greater than 0 when the request is invoked. Release or Autorealse must appear in pairs with new or retain.


Here's how to start talking about the main use. Because the class that we usually need to manage and use is usually inherited from node (Layer, Sprite, etc.).         Look at node's create function. In node's create, when new (count is set to 1) is called, Autorelease is called immediately and placed in Autoreleasepool.

(Autoreleasepool's source code will be analyzed later) so when you use create, do not use release or autorelease unless you manually retain it once.

But when you add a node1 to a node2. You can understand that Node1 's referenececount has been added once, but you don't need to do anything extra. Because when Node1 is removed or even if there is no remove operation. When Node2 is deconstructed (it will subtract 1 of his child's count). In other words, the entire engine manages Referencecount on its own initiative. Just want you not to manually retain.



2.2AutoreleasePool Source Code Analysis

void Autoreleasepool::clear () {#if defined (cocos2d_debug) && (Cocos2d_debug > 0)    _isclearing = true;# endif for    (const auto &obj: _managedobjectarray)    {        obj->release ();    }    _managedobjectarray.clear (); #if defined (cocos2d_debug) && (Cocos2d_debug > 0)    _isclearing = false;# endif

The clear function of Autorealse is to call the release function of its included ref, and then empty its managedobject. The question that needs to be resolved here is when the clear function is called. Open the director's source code
void Displaylinkdirector::mainloop () {    if (_purgedirectorinnextloop)    {        _purgedirectorinnextloop = False ;        Purgedirector ();    }    else if (! _invalid)    {        drawscene ();             Release the Objects        Poolmanager::getinstance ()->getcurrentpool ()->clear ();    }

When your app executes, each frame starts with a drawscene (). When the display finishes on various interfaces, it starts clear.
2.3 Poolmanager std::d eque<autoreleasepool*> _releasepoolstack;
Autoreleasepool *_curreleasepool;        Poolmanager manages all the Autorerealsepool and has a pointer to the current releasepool. It's worth noting that. I think 3.x here has a small bug.
poolmanager* poolmanager::getinstance () {    if (s_singleinstance = = nullptr)    {        s_singleinstance = new Poolmanager ();        ADD the first auto release pool        s_singleinstance->_curreleasepool = new Autoreleasepool ("Cocos2d autorelease PO Ol ");        S_singleinstance->_releasepoolstack.push_back (S_singleinstance->_curreleasepool);    }    return s_singleinstance;}


Poolmanager is a singleton mode that, when initialized for the first time, generates a autoreleasepool on its own and puts it into its own stack. But. When you open the Autoreleasepool constructor, you find that there is already a call to Poolmanager::getinstance ()->push (this); Through the debug trace. The author found that there are two autorealsepool at this time. The Poolmanager stack has two positions pointing to the same autorealsepool.

The feeling here should be a bug.


3. Summary 1.ref,autorealsepool.        Poolmanager is closely related to the 2.Ref retain, new should appear in pairs with release or Autorealse. 3.Node use.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Cocos2d-x Source Code Analysis: Ref (ccobject) source code Analysis Cocos2d-x memory Management policy

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.