Re-development of labor results, reproduced when please be sure to indicate the source: http://blog.csdn.net/haomengzhu/article/details/27704153
Factory method mode
The factory method is a classic design pattern in programming. It refers to defining only interfaces for creating objects in the base class and postponing the actual implementation to the subclass.
Here, we will promote it a little. It refers to all static functions that generate and return an object.
A classic factory method is like this:
Sprite * factorymethod () {sprite * ret = new sprite (); // perform necessary initialization operations on the RET object here return ret ;}
This seemingly normal code actually hides a problem:
The reference of the factory method to the RET object has ended when the function returns, but it has not released the reference to the RET, laying the Hidden Risk of Memory leakage.
However, it is obviously inappropriate to run release () before the function returns,
As this triggers the collection of objects, the returned object pointer becomes an error pointer.
The autorelease () method is very good at solving this problem.
At the end of this function, we have lost the reference to ret. In order to pass the RET object to the recipient, we need to perform an autorelease operation on it, even though we call the autorelease method,
But objects are not actually released until they are actively recycled from the pool (usually the Cocos2d-x will release their own active recycle pool between each frame ), the caller has enough time to retain the object to take over the reference permission of the RET object.
Therefore, the Cocos2d-x's Operating Mechanism cleverly ensures that objects in the recycle pool are not released before use is complete.
The factory method modified with autorelease () is as follows:
Sprite * factorymethod () {sprite * ret = new sprite (); // perform necessary initialization operations on the RET object here ret-> autorelease (); return ret ;}
The caller must release the object with caution after use;
When using the factory method to create an object, although the reference count is also 1, because the object has been placed in the recycle pool,
Therefore, the caller does not have the permission to reference this object unless we manually call retain () to obtain the permission to reference this object,
Otherwise, you do not need to release the object.
About object Value passing
When assigning an object to a pointer as a reference, in order to follow the principle of memory management,
We need to obtain the reference right of the new object and release the reference right of the old object.
In this case, the order of release () and retain () is particularly important.
First, let's look at the following code:
void SomeClass::setRef(Ref* other) {this->object->release();other->retain();this->object = other;}
The hidden danger here is that when other and object actually point to the same object, the first release () may trigger the collection of the object, which is obviously not what we want to see, therefore, you should first run retain () to ensure that the other object is valid, and then release the old object:
void SomeClass::setRef(Ref* other) {other->retain();this->object->release();this->object = other;}
There are many other feasible solutions:
For example, use the autorelease () method to replace the release () method, or infer whether the two objects are the same before the value assignment.
In Google's objective-C Programming specification, we recommend that you replace the release () method with the autorelease () method.
Note that you only need to call the release () method in two cases:
(1) You new A cocos2d: ref subclass object, such as ccsprite and cclayer.
(2) You get the pointer of the coccos2d: ref subclass object, and then call the retain method in your code.
Haomeng master friendly reminder:
The pointer can be release if you don't want to release it !!
9. cocos2dx 3.0 game development finding small three factory method mode and object passing value