*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************
One more shot. Memory management mechanism
1. The mechanism of simple statements
2. Code View mechanism
1. Jianyan-cocos2d-x's memory management mechanism
Speaking of memory management, this is a question, (⊙v⊙) well.
Take a look at each language. How do you manage memory?
--java:
The heap area is new to allocate memory. Recycled through garbage collection mechanisms.
(Detailed very complex, can look at this article---http://blog.csdn.net/tutngfei1129287460/article/details/7383480)
--c++:
New to create, delete to remove
--objective-c:
Method of using reference counting
Create a new obj, each of which has a retaincount.
In new, Alloc, retain, will let this retaincount+1.
If you do the release operation on this object, you will let retainCount-1.
So. Each object has two methods: retain and release.
When Retaincount is 0 o'clock, the object is released.
Cocos2d-x uses C + + to implement the OBJECTIVE-C management mechanism.
2. Code-view-cocos2d-x memory management mechanism
Create an object and look at this management mechanism.
To open the project, you can see it in the AppDelegate.cpp of the portal program:
Create a scene. It's an autorelease objectauto scene = Helloworld::createscene (); Run director->runwithscene (scene);
A scene was created. and execute. Track it in. Functions for creating scenes:
' Scene ' is an Autorelease object auto scene = Scene::create (); ' Layer ' is an Autorelease object auto layer = Helloworld::create ();
In the Createscene function. The creation of the scene and the creation of the layer are made.
is used by the Create method. Either scene or layer, they belong to the node sub-class. The same node is also a subclass of ref.
As previously mentioned, the ref class is the memory management mechanism of cocos2d-x.
Tracked to, ref class, able to discover:
<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 the 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 theRef ' 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>
。。。。。 It's long.
There are:
Retain method (add Reference),
Release method (reduce the reference),
Autorelease method (realizes self-release voluntarily)
Getreferencecount method (get reference count)
Ref (), ~ref () constructors and destructors
_referencecount This is the reference value.
Autoreleasepaul Friend class
_id,_luaid support for JS and LUA scripts
Look again, the detailed definition of this class,
Ability to find ref class in project directory: cocos2d/cocos/base/
can also be in, after VS2012 opened. The ref class is found in the external dependency, but the. h header files are located. We're going to see. cpp yo
But when this class is looking for, it's called Ccref.
--in the ref constructor, you can see:
Ref::ref (): _referencecount (1)//When the REF was created, the reference count of it is 1
In other words, whenever an object is created, its reference value is initialized to 1.
--when, run the Retain method
void Ref::retain () { Ccassert (_referencecount > 0, "Reference count should greater than 0"); ++_referencecount;}
The reference value + + is
--When, run the release method
void Ref::release () { Ccassert (_referencecount > 0, "Reference count should greater than 0"); --_referencecount;
The reference value--
--and, after release, to be inferred whether it is 0, or 0, delete this:
if (_referencecount = = 0) {#if defined (cocos2d_debug) && (Cocos2d_debug > 0) auto Poolmanager = Poolma Nager::getinstance (); if (!poolmanager->getcurrentpool ()->isclearing () && poolmanager->isobjectinpools (this)) { Ccassert (False, "the reference shouldn ' t be 0 because it's still in autorelease pool."); #endif Delete this; }
From this, cocos2d-x is controlling the life cycle of an object by reference counting.
So. Cocos2d-x when programming. Basically, DELETE statements are not used, only in classes that are not ref base classes or in ref classes.
Go back and look at the scene's Create method:
Scene *scene::create () { scene *ret = new Scene (); if (ret && ret->init ()) { ret->autorelease (); return ret; } else { cc_safe_delete (ret); return nullptr;} }
In this case, we'll start with a new scene and then run the scenario initialization method (init). Then add the current object to your own active release pool management,
This method defines:
ref* ref::autorelease () { poolmanager::getinstance ()->getcurrentpool ()->addobject (this); return this;}
In this way, we do not need to understand the code. And when the object is referenced, the release method is self-active.
--end
Well, it is here, I would like to use an example to execute a bit, but there are some problems.
We are leaving tomorrow. Not for a while. Alas..
References:
Sdhjob (Miss Shen)
*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************
Cocos2d-x 3.0 memory management mechanism