[Wood Cocos2d-x 037] retain and release how are they going to play?

Original link: http://www.cnblogs.com/lifesteven/p/3927225.html

[Wood Cocos2d-x 037] retain and release how are they going to play?

Category: stupid wood-the X-Cocos2D

retain and release waitin how to play?

 

Whistling, I have not posted a tutorial (if small: a rare quiet, and you came out for the scary = = wool), in fact, recently published in preparation for wood I do books. But seemingly not very well, I was really accumulate enough, the process of writing a book is a big pressure, feeling not write interesting characters come out (if small: Ow ,,,). Sure enough, or write some free blog? Hey ~

The most recent and not very close (if small: the book must not statements of these errors occur, so you just can not write Come = = ), many of my friends to retain knowledge seems a little vague, and today I will share with you about retain the knowledge of it ~

 

Mahogany wood stupid contribution, what? Fa? Is not it, is the heart -

Reproduced please specify the original address http://blog.csdn.net/musicvs/article/details/8689345

text:

 

 

 

1. Why The retain ?

C ++ and Java are not the same, Java has a very convenient garbage collection mechanism, when we do not need to use an object, it gives null value. The C ++ new after an object, when not in use typically requires delete off.

So, Cocos2d-x invented a set of memory management mechanism (if small: send your sister paper ...), in fact, red boy's blog is explained in detail Cocos2d-x memory management, I can not afford to do not want to repeat Explanation. (If small: Do you still write? = = )

Retain mean keep a reference, that is to say, if you want to keep a reference to an object, to avoid it being Cocos2d-x is released, it would have to call the object's retain function. (Small if: Why not retain it will be released?)

 

 

2. The real murderer autoRelease

Since the narrator asked me sincerely, I'll answer it openly (if small: I have no strength Tucao today, okay = = ).

Once an object is called autoRelease function, then the object is Cocos2d-x memory management mechanisms to the eye, if the object is unclaimed, then waiting for it to be released. (Small if: = = not too long Tucao, sometimes do not know what good spit).

 

3.  Look at the code the actual point

Having said that, or the bar code.

Create a Cocox2d-x project will take directly HelloWorldScene surgery, modify init function, add a code at the end:

  1. bool HelloWorld::init()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6.         / * Number of code have been omitted. . . . . . * /  
  7.   
  8.         testSprite = CCSprite::create("HelloWorld.png");  
  9.   
  10.         bRet = true;  
  11.     } while (0);  
  12.   
  13.     return bRet;  
  14. }  


 

(Small if: testSprite what stuff?)

testSprite is a member variable, plus in the header files can be:

  1. class HelloWorld : public cocos2d::CCLayer  
  2. {  
  3. public:  
  4.     virtual bool init();    
  5.     static cocos2d::CCScene* scene();  
  6.     void menuCloseCallback(CCObject* pSender);  
  7.     CREATE_FUNC(HelloWorld);  
  8. private:  
  9.     cocos2d :: CCSprite * test sprite;  
  10. };  


 

Then came the most critical, we modify menuCloseCallback function:

  1. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  2. {  
  3.     testSprite-> getPosition ();  
  4. }  


 

Now run the project, click on the button to see what is the situation?

(Small if: being given!)

If you know how to debug the project, then we menuCloseCallback function where the breakpoint, use debug mode to run the project and see testSprite objects:

(Small if: normal ah, what special?)

I am your sister paper, ah, positive! You just positive! (If small: Do not praise me so openly O O !)

 

 

We should be able to see a lot of non-normal data, the figure has been marked out with a red circle, which represents testSprite object is released, and now testSprite point to an unknown location.

This is very dangerous, and sometimes it does not complain immediately, but a sudden collapse at some point!

 

To solve this problem is very simple, modified again init function:

  1. bool HelloWorld::init()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6.         / * Number of code have been omitted. . . . . . * /  
  7.   
  8.         testSprite = CCSprite::create("HelloWorld.png");  
  9.   testSprite->retain();  
  10.     
  11.         bRet = true;  
  12.     } while (0);  
  13.   
  14.     return bRet;  
  15. }  


 

Run the project again, we will look at not being given? (If small: do not, why?)

Use debug mode to run the project again to see testSprite objects:

 

(If small: all is not normal! 0 !!)

零你妹纸= =(小若:为什么今天你总是抢我的对白O O!)

这次我们看到testSprite的数据明显正常了。

 

 

4. 原理来了

好了,唠叨了一大堆,还没有进入正题。

首先,要想让对象参与内存管理机制,必须继承CCObject类(CCNodeCCLayer等都继承了CCObject类)。

然后,调用对象的autoRelease函数,对象就会被Cocos2d-x的内存管理机制盯上,在游戏的每一帧,内存管理机制都会扫描一遍被盯上的对象,一旦发现对象无人认领,就会将对象杀死!(小若:嗷~残忍!)

如果不想让对象被杀死,那么就要调用对象的retain函数,这样对象就被认领了,一旦对象被认领,就永远不会被内存管理机制杀掉,是永远,一辈子。(小若:好朋友,一辈子= =

但,对象一辈子都不被释放的话,那么就会产生内存泄露,你试试加载一个占20M内存的对象一辈子不释放,不折腾死才怪~(小若:你去加载一个20M的对象本身就是闲的那个什么疼啊!)因此,当你不需要再使用这个对象时,就要调用对象的release函数,这是和retain对应的。一般可以在析构函数里调用release函数。

 

 

5. 实际情况

讲道理,大家都懂,但是,相信很多朋友在实际写代码的时候,还是会感觉很混乱。

比如,什么时候该retain?大家是不是发现,有时候不retain也不会报错?

其实这很简单,因为我们经常会在create一个对象之后,添加到层里,如:

testSprite = CCSprite::create("HelloWorld.png");

this->addChild(testSprite);

addChild函数就是导致大家混乱的凶手了,addChild函数会调用对象的retain函数,为什么它要调用对象的retain函数呢?因为你都把对象送给它当孩子了,它当然要认领这个对象了!(小若:我懂了,嗷!)

于是,当我们把对象addChildCCLayer时(不一定是CCLayerCCArrayCCNode都行),我们就不需要调用对象的retain函数了。

 

 

6. 那倒底什么时候要retain

说了这么多,还是没有说清楚,什么时候要调用对象的retain

很简单,当你把一个对象作为成员变量时,并且没有把对象addChild到另外一个对象时,就需要调用retain函数。

 

7. 最后的最后

一定要记住,必须要调用了对象的autoRelease函数之后,retainrelease函数才会生效,否则,一切都是徒劳。

因此,十分建议使用create的方式创建对象,如:

  1. CCSprite* CCSprite::create(const char *pszFileName)  
  2. {  
  3.     CCSprite *pobSprite = new CCSprite();  
  4.     if (pobSprite && pobSprite->initWithFile(pszFileName))  
  5.     {  
  6.         pobSprite->autorelease();  
  7.         return pobSprite;  
  8.     }  
  9.     CC_SAFE_DELETE(pobSprite);  
  10.     return NULL;  
  11. }  


 

这些就是retain表面上的知识了,至于retain源码级别的解说,请到红孩儿的博客吧,强烈推荐~

 

好了,不唠叨了~困喇,睡大觉去~~

 

 

转载于:https://www.cnblogs.com/lifesteven/p/3927225.html

retainrelease倒底怎么玩?

 

呼呼,好久没有发布教程了(小若:难得清静了,你为毛又出来吓人= =),其实最近木头我在准备出版书籍的事情。但是貌似不太顺利,果然我还是积累不够,写书的过程压力好大,感觉写不出有趣的文字出来(小若:嗷、、、)。果然还是在博客写自由一些?嘿嘿~

最近以及最不是很近(小若:书里一定不能出现这些错误的语句,所以你才写不出来吧= =),不少朋友对retain的认识似乎有点模糊,今天我就和大家分享一下关于retain的知识吧~

 

笨木头花心贡献,啥?花心?不呢,是用心~

转载请注明,原文地址: http://blog.csdn.net/musicvs/article/details/8689345

正文:

 

 

 

1. 为什么会有retain

C++Java不一样,Java有一套很方便的垃圾回收机制,当我们不需要使用某个对象时,给它赋予null值即可。而C++new了一个对象之后,不使用的时候通常需要delete掉。

于是,Cocos2d-x就发明了一套内存管理机制(小若:发你妹纸。。。),其实红孩儿的博客很详细地解释了Cocos2d-x的内存管理机制,我没有能力也不想重复解释。(小若:那你还写?= =

Retain的意思是保持引用,也就是说,如果想保持某个对象的引用,避免它被Cocos2d-x释放,那就要调用对象的retain函数。(小若:为什么不retain就会被释放?)

 

 

2. 真正的凶手autoRelease

既然旁白诚心诚意地问我,那我就光明正大地回答吧(小若:我今天没力气吐槽,好吧= =)。

一旦调用对象的autoRelease函数,那么这个对象就被Cocos2d-x的内存管理机制给盯上了,如果这个对象没人认领,那就等着被释放吧。(小若:= =太久没吐槽,一时不知道吐什么好)。

 

3. 看代码实际点

说了这么多,还是上代码吧。

创建一个Cocox2d-x的项目,就直接拿HelloWorldScene开刀,修改init函数,在最后添加一句代码:

  1. bool HelloWorld::init()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6.         /* 很多代码被省略了。。。。。。 */  
  7.   
  8.         testSprite = CCSprite::create("HelloWorld.png");  
  9.   
  10.         bRet = true;  
  11.     } while (0);  
  12.   
  13.     return bRet;  
  14. }  


 

(小若:testSprite是什么东东?)

testSprite是一个成员变量,在头文件里加上就可以了:

  1. class HelloWorld : public cocos2d::CCLayer  
  2. {  
  3. public:  
  4.     virtual bool init();    
  5.     static cocos2d::CCScene* scene();  
  6.     void menuCloseCallback(CCObject* pSender);  
  7.     CREATE_FUNC(HelloWorld);  
  8. private:  
  9.     cocos2d::CCSprite* testSprite;  
  10. };  


 

然后,最关键的来了,我们修改menuCloseCallback函数:

  1. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  2. {  
  3.     testSprite->getPosition();  
  4. }  


 

现在,运行项目,点击按钮,看看是什么情况?

(小若:报错了!)

如果大家知道怎么调试项目的话,我们在menuCloseCallback函数里断点,用调试模式运行项目,看看testSprite对象:

(小若:很正常啊,有什么特别的?)

正你妹纸啊,正!你才正!(小若:不要这么光明正大地赞我O O!)

 

 

我们应该能看到不少非正常数据,图中已经用红色圈圈标出来了,这代表testSprite对象被释放了,现在testSprite指向未知的位置。

这是很危险的,有时候它不会立即报错,但是在某个时刻突然崩溃!

 

要想解决这个问题,很简单,再次修改init函数:

  1. bool HelloWorld::init()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6.         /* 很多代码被省略了。。。。。。 */  
  7.   
  8.         testSprite = CCSprite::create("HelloWorld.png");  
  9.   testSprite->retain();  
  10.     
  11.         bRet = true;  
  12.     } while (0);  
  13.   
  14.     return bRet;  
  15. }  


 

再次运行项目,看看还会不会报错?(小若:不会了,为什么?)

再次用调试模式运行项目,看看testSprite对象:

 

(小若:不正常!都是0!!)

零你妹纸= =(小若:为什么今天你总是抢我的对白O O!)

这次我们看到testSprite的数据明显正常了。

 

 

4. 原理来了

好了,唠叨了一大堆,还没有进入正题。

首先,要想让对象参与内存管理机制,必须继承CCObject类(CCNodeCCLayer等都继承了CCObject类)。

然后,调用对象的autoRelease函数,对象就会被Cocos2d-x的内存管理机制盯上,在游戏的每一帧,内存管理机制都会扫描一遍被盯上的对象,一旦发现对象无人认领,就会将对象杀死!(小若:嗷~残忍!)

如果不想让对象被杀死,那么就要调用对象的retain函数,这样对象就被认领了,一旦对象被认领,就永远不会被内存管理机制杀掉,是永远,一辈子。(小若:好朋友,一辈子= =

但,对象一辈子都不被释放的话,那么就会产生内存泄露,你试试加载一个占20M内存的对象一辈子不释放,不折腾死才怪~(小若:你去加载一个20M的对象本身就是闲的那个什么疼啊!)因此,当你不需要再使用这个对象时,就要调用对象的release函数,这是和retain对应的。一般可以在析构函数里调用release函数。

 

 

5. 实际情况

讲道理,大家都懂,但是,相信很多朋友在实际写代码的时候,还是会感觉很混乱。

比如,什么时候该retain?大家是不是发现,有时候不retain也不会报错?

其实这很简单,因为我们经常会在create一个对象之后,添加到层里,如:

testSprite = CCSprite::create("HelloWorld.png");

this->addChild(testSprite);

addChild函数就是导致大家混乱的凶手了,addChild函数会调用对象的retain函数,为什么它要调用对象的retain函数呢?因为你都把对象送给它当孩子了,它当然要认领这个对象了!(小若:我懂了,嗷!)

于是,当我们把对象addChildCCLayer时(不一定是CCLayerCCArrayCCNode都行),我们就不需要调用对象的retain函数了。

 

 

6. 那倒底什么时候要retain

说了这么多,还是没有说清楚,什么时候要调用对象的retain

很简单,当你把一个对象作为成员变量时,并且没有把对象addChild到另外一个对象时,就需要调用retain函数。

 

7. 最后的最后

一定要记住,必须要调用了对象的autoRelease函数之后,retainrelease函数才会生效,否则,一切都是徒劳。

因此,十分建议使用create的方式创建对象,如:

  1. CCSprite* CCSprite::create(const char *pszFileName)  
  2. {  
  3.     CCSprite *pobSprite = new CCSprite();  
  4.     if (pobSprite && pobSprite->initWithFile(pszFileName))  
  5.     {  
  6.         pobSprite->autorelease();  
  7.         return pobSprite;  
  8.     }  
  9.     CC_SAFE_DELETE(pobSprite);  
  10.     return NULL;  
  11. }  


 

These are retain knowledge on the surface, as to retain source-level explanation, please go to Red Boy's blog it is strongly recommended ~

 

Well, do not talk to me ~ sleepy La, go to sleep ~ ~

 

Guess you like

Origin blog.csdn.net/weixin_30747253/article/details/94925765