Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the thing,</p> <p>Object of the <code>class Ref</code> or any class derived from it has a variable <code>_retainCount</code> which represents the scope of the object.</p> <p>Also there is an <code>autorelease</code> pool which is similar to the garbage collector in <code>java</code>. The object which is added to this <code>autorelease</code> pool will be deleted at the end of the frame. Unless its <code>_retainCount!=0</code></p> <p>Now when you create new object of <code>Ref</code> or derived class using the create method it is already added to the <code>autorelease</code> pool and you don't need to <code>release</code> it or <code>delete</code> it anywhere. As you can see in the create function of <code>Node</code> below.</p> <pre><code>Node * Node::create() { Node * ret = new (std::nothrow) Node(); if (ret &amp;&amp; ret-&gt;init()) { ret-&gt;autorelease(); } else { CC_SAFE_DELETE(ret); } return ret; } </code></pre> <p>But when you create new object using 'new' you definitely need to <code>delete</code> it after its use is over. Though it is not recommended to use New to <code>allocate</code> the objects of <code>cocos2d</code> classes. Rather use create.</p> <pre><code>Node* temp=Node::create(); </code></pre> <p>then,</p> <pre><code>temp-&gt;retain(); //your work... temp-&gt;release(); </code></pre> <p>or</p> <pre><code>Node* temp=Node::create(); Node* tempChild=Node::create(); temp-&gt;addChild(tempChild); //your work... temp-&gt;removeFromParent(); </code></pre> <p>Second thing,</p> <p>when your object is added to the <code>autorelease</code> pool but you need to increase its scope you could just retain it , this increments its retain count by one and then you have to manually release it i.e, decrement its retain count after its use is over.</p> <p>Third Thing,</p> <p>Whenever you add child your object it is retained automatically but you don't need to <code>release</code> it rather you remove it from the parent as mentioned above.</p> <p>Official documentation is @link below.</p> <p>[<a href="http://www.cocos2d-x.org/wiki/Reference_Count_and_AutoReleasePool_in_Cocos2d-x#Refrelease-retain-and-autorelease][1]" rel="noreferrer">http://www.cocos2d-x.org/wiki/Reference_Count_and_AutoReleasePool_in_Cocos2d-x#Refrelease-retain-and-autorelease][1]</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload