Cocos2d-x Study Notes (4)

Source: Internet
Author: User
The existing Intelligent Memory Management Technology in Cocos2d-x: (1) reference counting, there are the problem of fragmentation and management; (2) garbage collection. The Cocos2d-x cleverly utilizes the previous reference counting mechanism. In the CCObject. h header file, we can see the definition of CCObject classCC_DLLCCObject: publicCCCopying {public:

The existing Intelligent Memory Management Technology in Cocos2d-x: (1) reference counting, there are the problem of fragmentation and management; (2) garbage collection. The Cocos2d-x cleverly utilizes the previous reference counting mechanism. In the CCObject. h header file, we can see the definition of CCObject class CC_DLL CCObject: public CCCopying {public ://

Memory Management in Cocos2d-x

Existing smart memory management technology: (1) reference counting, there are problems with heap fragmentation and management complexity; (2) garbage collection.

The Cocos2d-x cleverly utilizes the previous reference counting mechanism, in the CCObject. h header file, see the definition of CCObject

Class CC_DLL CCObject: public CCCopying {public: // object id, CCScriptSupport need public m_uID object id, use unsigned int m_uID in the script engine; reference id in Lua, used by the script // Lua reference id int m_nLuaID; protected: // count of references unsigned int m_uReference; // reference quantity // count of autorelisted unsigned int m_uAutoReleaseCount; // whether to set it to autorelease and automatically recycle the pool public: CCObject (void); virtual ~ CCObject (void); // void release (void), a virtual destructor, is referenced elsewhere. m_uReference automatically adds 1 void retain (void); // The reference ends, m_uReference automatically minus 1
CCObject * autorelease (void); // automatically puts the object into the recycle pool. When the recycle pool itself is released, execute the release () method on all objects in the pool.
CCObject * copy (void); bool isSingleReference (void); unsigned int retainCount (void); virtual bool isEqual (const CCObject * pObject); virtual void update (float dt) {CC_UNUSED_PARAM (dt) ;}; friend class CCAutoreleasePool ;};

After an autorelease () operation is performed, the object reference is not immediately released. Before the next frame starts, the object is released.

However, considering the efficiency issue, if a large number of autorelease objects are generated during a frame process, resulting in a reduction in the collection pool performance, you can manually set a collection pool where autorelease () is intensive. As follows:

CCPoolManger::sharedPoolManager()->push();for(int i = 0;i < n; i++){    CCString *dataItem = CCString::createWithFormat(”%d“,Data[i]);    stringArray->addObject(dataItem);}CCPoolManager::sharedPoolManager()->pop();
The Code executes n cycles and creates an autorelease object CCString each time. To maintain the performance of the recycle pool, a new recycle pool is created using the push method before the loop, after the loop ends, use the pop method to release the recycle pool.

Factory method:

The factory method is a classic design pattern in programming. It refers to the interface that defines the object to be created in the class and delays the actual implementation to the subclass. Analyze the following code:

CCObject* factoryMethod(){    CCObject* ret = new CCobject();    return ret;}

The memory indicated by ret is released when the returned result is ret. Use autorelease () to solve this problem. Although autorelease is called, the object is not directly released, but the recycle pool is released after a frame ends. Modified code
CCObject* factoryMethod(){    CCObject* ret = new CCobject();    ret->autorelease();    return ret;}
Although the reference count of the object created using the factory method is also 1, the caller does not have the permission to reference the object because the object has been placed in the recycle pool, unless we think that calling retain () to obtain the reference permission. Otherwise, you do not need to release the object.

About transferring values:

When assigning an object to a pointer as a reference, in order to follow the memory management principle, we need to obtain the reference right of the new object and release the object's reference right. The order of release () and retain () is particularly important.

The following code:

// Error code void SomeClass: setObject (CCObject * other) {this-> object-> release (); other-> retain (); this-> object = other ;} // The Code void SomeClass: setObject (CCObject * other) {other-> retain (); this-> object-> release (); this-> object = other ;}
The first error. When the other and object point to the same object, the first release recycles the object. Therefore, run retain () to ensure that the other object is valid and release the old object.

Autorelease () can only be released automatically. If the number of objects released exceeds the expected number, this error will not be detected, the cluster crashes only when the automatic release pool is released. Locating errors is very difficult. Therefore, in development, try to avoid misuse of autorelease () and use it only when the factory method is unavailable. Try to release the object reference with release.

Containers: CCArray, CCdictionary, If you directly use STL containers, developers need to carry out tedious memory management operations, the Cocos2d-x of this process is encapsulated.

Related auxiliary macros: Included in the header file CCPlatformMacro. h

Memory management-related macros in the Cocos2d-x
Macro Description
CC_SAFE_DELETE (p) Delete a c ++ object p using the delete operator. If p is NULL, no operation is performed.
CC_SAFE_DELETE_ARRAY (p) Delete a c ++ array p using the delete [] Operator. If p is NULL, no operation is performed.
CC_SAFE_FREE (p) Use the free () function to delete p. If p is NULL, no operation is performed.
CC_SAFE_RELEASE (p) Release a reference of p using the release () method. If p is NULL, no operation is performed.
CC_SAFE_RELEASE_NULL (p) Release a reference of p using the release () method, and assign p to NULL. If p is already NULL, no operation is performed.
CC_SAFE_RETAIN (p) Use the retain () method to add a reference to p. If p is NULL, no operation is performed.

Cocos2d-x memory management principles:

(1) The program segment must run retain () and release () or autorelease () in pairs to start and end object references.

(2) Before the factory method is returned, the reference to this object should be ended through autorelease.

(3) When passing values to an object, special situations with the same old and new objects should be taken into account.

(4) Try to use release () instead of autorelease () to release object references to ensure optimal performance.

(5) When saving the CCObject subclass object, should strictly use the container provided by the Cocos2d-x, avoid using STL container, the object must be saved as a pointer.

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.