Cocos2d-x Study Notes (14) CCAutoreleasePool

Source: Internet
Author: User

Cocos2d-x Study Notes (14) CCAutoreleasePool

Original article, reproduced please indicate the source: http://blog.csdn.net/sfh366958228/article/details/38964637


Preface

After learning so much content, almost all the controls involve the memory management class CCAutoreleasePool. So this time, let's take a look at the CCAutoreleasePool, starting with the CCObject autorelease method.


CCObject: autorelease

CCObject * CCObject: autorelease (void) {// Add the current object to the memory pool CCPoolManager: sharedPoolManager ()-> addObject (this ); // set the current object to be managed by the memory pool m_bManaged = true; return this ;}
The autoRelease class is not complex. From the comments in the code, we can see that the main operation is CCPoolManager, that is, CCAutoreleasePool.


CCAutoreleasePool. h

Class CC_DLL CCAutoreleasePool: public CCObject {// save all objects added to the release pool. // note that CCArray stores the objects by retain, the application count will increase by 1 CCArray * m_pManagedObjectArray; public: CCAutoreleasePool (void );~ CCAutoreleasePool (void); // Add the object to the automatic release pool void addObject (CCObject * pObject); // remove the object from the automatic release pool void removeObject (CCObject * pObject ); // release the objects in the automatic release pool void clear () ;}; class CC_DLL CCPoolManager {// used to store the queue CCArray * m_pReleasePoolStack of the automatic release pool; // The current automatic release pool points to the End Node CCAutoreleasePool * m_pCurReleasePool; // gets the current automatic release pool CCAutoreleasePool * getCurReleasePool (); public: CCPoolManager (); ~ CCPoolManager (); // clear all automatic release pools void finalize (); // Add an automatic release pool void push (); // remove an automatic release pool, remove void pop () from the current automatic release pool; // Add the object to the current automatic release pool void removeObject (CCObject * pObject ); // remove void addObject (CCObject * pObject) from the current automatic release pool. // obtain the automatically released pool manager, in the code of the entire program, all the places where CCPoolManager needs to be used are obtained through this function. // The new memory manager should not use static CCPoolManager * sharedPoolManager (); // clear the current Memory Manager, release the automatic release pool, and release all objects in the pool. static void purgePoolManager (); friend class CCAutoreleasePool ;};

CCAutoreleasePool. cpp

// Static member variable, saving the automatic release pool manager used by the entire program static CCPoolManager * s_pPoolManager = NULL; CCAutoreleasePool: CCAutoreleasePool (void) {// initialize the queue, used to save the object m_pManagedObjectArray = new CCArray (); m_pManagedObjectArray-> init ();} CCAutoreleasePool ::~ CCAutoreleasePool (void) {CC_SAFE_DELETE (m_pManagedObjectArray);} // Add the object to the automatically released pool void CCAutoreleasePool: addObject (CCObject * pObject) {// Save the object retain to the queue m_pManagedObjectArray-> addObject (pObject); // here, the application count of the object should be at least 2 CCAssert (pObject-> m_uReference> 1, "reference count shoshould be greater than 1"); // call the release method of the object to reduce the reference count by 1 // The release method is implemented in CCObject, check that the reference count is 0 after the reference count is reduced by 1. If it is 0, delete this object pObject-> relearese ();/ /No ref count, in this case autorelease pool added .} void CCAutoreleasePool: removeObject (CCObject * pObject) {// removes an object from the Auto Release pool so that the object is not managed by the auto release pool, of course, the second parameter will not be automatically released // false, because addObject is not used to increase the reference count. Here, the application count cannot be changed m_pManagedObjectArray-> removeObject (pObject, false);} // clear the automatic release pool and release all objects in the pool. void CCAutoreleasePool: clear () {if (m_pManagedObjectArray-> count ()> 0) {// CCAutoreleasePool * pReleasePool; # ifdef _ DEBUG Int nIndex = m_pManagedObjectArray-> count ()-1; # endif CCObject * pObj = NULL; CCARRAY_FOREACH_REVERSE (m_pManagedObjectArray, pObj) {if (! PObj) break; pObj-> m_bManaged = false; // (* it)-> release (); // delete (* it); # ifdef _ DEBUG nIndex --; # endif} // remove all objects in the queue and call the release method m_pManagedObjectArray-> removeAllObjects ();} of all objects in sequence ();}} // release // CCPoolManager // ------------------------------------------------------------------------ // obtain the CCPoolManager * CCPoolManager: sharedPool Manager () {if (s_pPoolManager = NULL) {s_pPoolManager = new CCPoolManager ();} return s_pPoolManager;} void CCPoolManager: purgePoolManager () {consumer (s_pPoolManager);} CCPoolManager:: CCPoolManager () {// initialize the automatic release pool queue m_pReleasePoolStack = new CCArray (); m_pReleasePoolStack-> init (); // The current automatic release pool is empty m_pCurReleasePool = 0 ;} CCPoolManager ::~ CCPoolManager () {// release all automatic release pools finalize (); // we only release the last autorelease pool here m_pCurReleasePool = 0; m_pReleasePoolStack-> removeObjectAtIndex (0 ); CC_SAFE_DELETE (m_pReleasePoolStack);} void CCPoolManager: finalize () {// clear all Auto Release pools in the Auto Release pool queue if (m_pReleasePoolStack-> count ()> 0) {// CCAutoreleasePool * pReleasePool; CCObject * pObj = NULL; CCARRAY_FOREACH (m_pReleasePoolStack, pObj) {if (! PObj) break; CCAutoreleasePool * pPool = (CCAutoreleasePool *) pObj; pPool-> clear () ;}} void CCPoolManager: push () {// Add a new auto release pool to the Auto Release pool queue, use CCAutoreleasePool * pPool = new CCAutoreleasePool (); // ref = 1 m_pCurReleasePool = pPool; m_pReleasePoolStack-> addObject (pPool); // ref = 2 pPool-> release (); // ref = 1} void CCPoolManager: pop () {// clear the automatic release pool queue, leaving only the first automatic release pool in the queue. // The remaining automatic release pool The objects in the pool need to be cleared. // This function is used to automatically release the pool manager and automatically release the objects in the pool. if (! M_pCurReleasePool) {return;} int nCount = m_pReleasePoolStack-> count (); // clear the current automatic release pool m_pCurReleasePool-> clear (); if (nCount> 1) {// if more than one Auto Release pool exists in the Auto Release pool queue // clear the Auto Release pool at the end and remove m_pReleasePoolStack-> removeObjectAtIndex (nCount-1); m_pCurReleasePool = (CCAutoreleasePool *) m_pReleasePoolStack-> objectAtIndex (nCount-2) ;}} void CCPoolManager: removeObject (CCObject * pObject) {// remove the CCAssert (m _ PCurReleasePool, "current auto release pool shocould not be null"); m_pCurReleasePool-> removeObject (pObject);} void CCPoolManager: addObject (CCObject * pObject) {// Add the object to the current automatic release pool getCurReleasePool ()-> addObject (pObject);} CCAutoreleasePool * CCPoolManager: getCurReleasePool () {// obtain the current Auto Release pool. // if the current Auto Release pool is empty, the auto release pool queue is empty. // use the push method, add new Auto Release pool if (! M_pCurReleasePool) {push ();} CCAssert (m_pCurReleasePool, "current auto release pool shocould not be null"); return m_pCurReleasePool ;}
The manager of the automatic release pool uses the pop method to call the release method to release all objects in the current automatic release pool. When is the pop method called? In fact, the answer is in the mainloop method we have seen before:

Void CCDisplayLinkDirector: mainLoop (void) {if (m_bPurgeDirecotorInNextLoop) {m_bPurgeDirecotorInNextLoop = false; purgeDirector ();} else if (! M_bInvalid) {// draw scene drawScene (); // release the automatic release pool CCPoolManager: sharedPoolManager ()-> pop ();}}
After each frame is drawn, the current automatic release pool will be cleared. All objects that call the autorelease operation will be called the release method to reduce the reference count. If the created object calls autorelease, the reference count of this object will be reduced by 1 when the pool is automatically released after the frame is drawn later, if this object is not
Retain, it will be released.
In terms of object usage, to ensure that the object can be correctly released, you must always know the reference count of this object, but it is difficult to do this in many cases, unless you are familiar with all the interfaces and know whether retain or release operations are performed on the current object, if you cannot do so, you can follow the principles in the cocos2d-x framework to do so.
1. As an object passed in as a parameter, if you want to use or manage it for a long time, please retain, remember to release when not needed
2. The object passed in as a parameter is not created by you or retain. If you are not sure where the object comes from, do you know that you will release it? Please do not call release at will.
3. If you open the automatic release pool and create an object without calling autorelease, delete the object directly when it is not in use. This method is not safe, unless the object you create is only used by you and is not retain by other objects. However, it does not mean that you cannot manage objects by yourself in new or delete mode.
4. Create a new object and call the autorelease method of the object. To use it for a long time, use the retain method (including addChild to itself and addObject to a CCArray ), use the release method when clearing (removeChild, CCArray removeObject, etc)

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.