At the beginning of this year, I entered a game company and officially started learning about the game engine. The previous IOS learning is still of great help to the current game engine learning. Although C ++ is used, however, we can always feel the huge impact of the IOS framework on cocos2dx.
Since we have been using arc before, we are not familiar with the release, retain, and C ++. It's okay. Let's get started again.
Let's get started with cocos2dx memory management.
In C ++, new and delete are a pair, while those inherited from C include malloc and free. Delete has one more class destructor than free, therefore, it is best to use the new and delete functions in the class. In the cocos2dx framework, we often see the create function, such as cclayer, ccsprite, after entering the create function, we can see that it is actually autorelease after new, and what has autorelease done? Fortunately, the source code is open, and it is easy to follow it in. ccpoolmanager: sharedpoolmanager ()-> addobject (this); it was originally added to the pool, and then went to addobject to see it, the added ccautoreleasepool is displayed. So how is this pool used?
You can see that the appcontroller. MM file contains the traditional IOS entry functions,
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary
*) Launchoptions
You can see a static variable outside the function:
Staticappdelegate s_sharedapplication
Static variables should be created first, so we can see that
Ccapplication: ccapplication ()
{
Cc_assert (! Sm_psharedapplication );
Sm_psharedapplication = this;
}
Then proceed to the entry function. You can see the last sentence in the entry function.
Cocos2d: ccapplication: sharedapplication ()-> Run ();
This is the key.
Boolappdelegate: applicationdidfinishlaunching (). At this time, you need to use ccdirector.
Ccdirector: shareddirector (). If you are familiar with it, you don't have to explain it. When you use it for the first time, you will call the init function of director, in the init function, you will see
Ccpoolmanager: sharedpoolmanager ()-> push (); initialization of the pool starts. There is a pool stack m_preleasepoolstack in ccpoolmanager. The pool stack stores one pool, the object we add is put into the current pool.
When
Boolappdelegate: After the applicationdidfinishlaunching () function is successfully completed, it enters the main loop. Anyone who has learned VC knows that if you want to prevent the program from exiting, a main loop will inevitably exist. Function
[[Ccdirectorcallershareddirectorcaller] startmainloop]; this loop is started and can be seen in startmainloop.
Displaylink = [nsclassfromstring (@ "cadisplaylink") displaylinkwithtarget: selfselector: @ selector (docaller :)]; is another key point,
Someone may ask, isn't displaylink an ID type? Do you see the nsclassfromstring? In fact, it is the cadisplaylink. What is this class used? It is a constantly refreshed class, And the refresh frequency is determined by the frame rate setting. Here we mainly talk about the docaller function. In this callback function, we will perform the following operations:
If (m_bpurgedirecotorinnextloop)
{
M_bpurgedirecotorinnextloop = false;
Purgedire ();
}
Else
If (! M_binvalid)
{
Drawscene ();
// Release the objects
Ccpoolmanager: sharedpoolmanager ()-> POP ();
}
That is to say, each primary loop will have a pool cleaning to delete all the objects with zero count. This is the memory management method of autorelease in cocos2dx.
After talking so much about it, I just got into touch with the key points. Haha, it seems that it is still a bit cool.
That is how to use release and retain.
Summary:
1. When using CREATE, they are all added with autorelease, so if you need to use it, remember to retain it;
2. If new is used, remember to use release when not needed;
3. In general, ccnode of cocos2dx has create, for example, ccarray does not have to worry about release issues;
4. Do not forget to release the memory of the C ++ functions like vector;
5. It is best not to mix them. If you know them well, OK, you can;
6. For unified format, I generally prefer to create the CREATE FUNCTION myself.
The microcloud engine is similar.