Forum > C++ > Understading memory collector: release + new

Understading memory collector: release + new

By Darkying Posted 2012-02-12 20:53 Comments 2
Darkying
  • Posts: 13

Hello,
I’ve been using this awesome library for a while and had a doubt about how the release / new mechanism works. For example for this code:

class A : public CCNode
{
// Some members & methods
}

class B : public CCNode
{
// Some members & methods
void func();
}

void B::func()
{

A a = new A;
this~~>addChild;

}
What happens when I call
B~~>removeAllChildren(true) // with cleanup = true
?

The A object is added to the B object, but not created using the release / retain / autorelease mechanism.
Does this provoke a memory leak?
Should I call delete getChildByTag; in the B destructor?

Thank you for your help! :D

walzer
  • Posts: 1735
  • Location: Xiamen, China

#1 RE: 2012-02-13 01:08

A a = new A;  // a.m_uRefCount = 1
this->addChild(a,0,0);   // b holds a, so a.m_uRefCount = 2
a->release();   // paring to new A 

So next time when you B->removeAllChildren(true), a.m_uRefCount will reduce to 0 and be released, you don’t need to call an extra delete.
You can refer to http://www.cocos2d-x.org/boards/6/topics/678?r=696

Less is More. Keep Focus.

Darkying
  • Posts: 13

#2 RE: 2012-02-13 09:09

Thank you very much. That helped a lot.

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps