Need to release CCParticles???

  • I have some question about CCParticles.
    As I know, I don’t need to call release() for most of my CCObject. Example, when I create CCSprite::SpriteWithFile, I don’t need to release it, and it can be autorelease, as soon as I don’t use it anymore.
    But in performance test, I see you call particles~~>release,although it have autorelease in init method. So when I need to call release ?
    And in particles.cpp, you create m_emitter,retain it and only release in destractor. I don’t understand.
    ~~ In my game,I create a CCParticlesQuard mParticles, add this to other CCNode like mParent, and call mParticles~~>release after call mParent~~>addChild(mParticles).
    Then if I call mParent~~>cleanup, it have error with call stack:
    <pre>
    CCArray::count Line 106 //Error because NULL pointer
    CCNode::arratMakeObjectsPerformSelector*func) Line 110
    CCNode::cleanup Line 518 //don’t know what it be, may be CCParticlesQuard
    CCNode::arratMakeObjectsPerformSelector*func) Line 118
    CCNode::cleanup Line 518 //it’s mParent
    </pre>
    So when I need to call release? For every object,not only CCParticles. I feel I don’t understand this.
    Sometime I see you call m_scene~~>release(),too.
    And in my example,why I can’t call cleanup() on mParent?
  • one more question. If use one type Particle, but need to create more of it, how I can do? I feel it need more time to call
    CCParticleSystem::particleWithFile, although I only use one plist file.
    more in topic :
    http://www.cocos2d-x.org/boards/6/topics/7736
    anyway, I break point and don’t see it come to CCParticleSystemQuad::~CCParticleSystemQuad(). Does it destroy auto when it finish run if i don’t call release?
    (If i call release,it have error).
  • I try to call release, but it cause more error with NULL pointer.
    I create some break point on destructor of CCParticleSystemQuad and CCParticleSystem. And I only jump to it when my game exit, not when particle finish run.
    In my game I need create so much Particle. Can it make memory leak?
  1. The object created from static functions (e.g. CCSprite::SpriteWithFile) don’t need to be invoked ‘release’ if you don’t do a ‘retain’ operation.
  2. About invoking mParent->cleanup() causes crash, could you provide a simple demo code for us to analyze.
  3. You should invoke CCParticleSystem::particleWithFile for each instance.
  4. It can be stopped at the break point in the function ‘CCParticleSystemQuad::~CCParticleSystemQuad()’ on VS2008. Which cocos2d-x version you use?

Which platform you debug your application on?

Rubby Entertainment wrote:

  • I try to call release, but it cause more error with NULL pointer.
    I create some break point on destructor of CCParticleSystemQuad and CCParticleSystem. And I only jump to it when my game exit, not when particle finish run.
    In my game I need create so much Particle. Can it make memory leak?
  1. The object created from static functions (e.g. CCSprite::SpriteWithFile) don’t need to be invoked ‘release’ if you don’t do a ‘retain’ operation.
    So it mean something:
CCParticleSystem* m_particle = CCParticleSystem::particleWithFile("effect.plist");//static, don't need to release

And

CCParticleSystem* m_particle = new CCParticleSystem();
m_particle->initWithFile("effect.plist");
m_particle->release();//need to release,right?

And with CCSprite,too?

CCSprite* m_sprite = new CCSprite();
m_sprite->initWithFile("image.png");
m_sprite->release();
//need to call release too?

one question,I need to call release() or autorelease()?
Because I see if i use CCParticleSystem::particleWithFile, it have call autorelease(),not only release().

  • For my error now,I use static function,so I don’t need to call release. Thank you.

  • But I still don’t see i jump to destructor when particle finish run.
    For example,In helloWorld default project,add a function

    void HelloWorld::createEffect()
    {
    struct
    {
    int operator()(int min,int max)
    {
    int tg = rand();
    int range = max - min + 1;
    int value = min + tg%range;
    return value;
    }
    }randomInt;

      srand(time(0));
      CCSize size = CCDirector::sharedDirector()->getWinSizeInPixels();
      for(int i = 0; iaddChild(effect);
          effect->setPosition(ccp(x,y));
          effect->setPositionType(kCCPositionTypeRelative);
      }
    

    }

My effect have duration is 5 second. After that, I don’t see it in screen,but it don’t call destructor. Only if I exit my app,it jump to it.
So it leak memory if I call createEffect so much,right?

If you want particle to be auto-removed, you should invoke ‘effect->setIsAutoRemoveOnFinish(true);’.

Oh,I really don’t know. Thank you so much.
And my question

So it mean something:
>
CCParticleSystem* m_particle = CCParticleSystem::particleWithFile(“effect.plist”);//static, don’t need to release
>
>
And
>
CCParticleSystem* m_particle = new CCParticleSystem();
m_particle~~>initWithFile;
m_particle~~>release();//need to release,right?
>
>
And with CCSprite,too?
>
CCSprite* m_sprite = new CCSprite();
m_sprite~~>initWithFile;
m_sprite~~>release();
//need to call release too?
>
>
one question,I need to call release() or autorelease()?
Because I see if i use CCParticleSystem::particleWithFile, it have call autorelease(),not only release().

You are right, if you create an object with keyword ‘new’, you should manage the memory yourself. When you don’t want to use it ,you need to invoke ’obj~~>release’.:slight_smile:
Rubby Entertainment wrote:

Oh,I really don’t know. Thank you so much.
And my question
> So it mean something:
>
> CCParticleSystem* m_particle = CCParticleSystem::particleWithFile;//static, don’t need to release
>
>
> And
>
> CCParticleSystem* m_particle = new CCParticleSystem;
> m_particle~~>initWithFile(“effect.plist”);
> m_particle~~>release;//need to release,right?
>
>
> And with CCSprite,too?
>
> CCSprite* m_sprite = new CCSprite;
> m_sprite~~>initWithFile(“image.png”);
> m_sprite->release();
> //need to call release too?
>
>
> one question,I need to call release() or autorelease()?
> Because I see if i use CCParticleSystem::particleWithFile, it have call autorelease(),not only release().

Sorry for ask so much.
Should I call release or autorelease?
I think I can call autorelease and It will be destroy auto after I have nothing use it,right?
If i only call release, the CCPoolManager don’t add it, and it can’t destroyed auto?

I think these two methods are all ok.

CCSprite* m_sprite = new CCSprite(); //ref=1;
m_sprite->initWithFile("image.png"); 
yourLayer->addChild(m_prite); // ref = 2;
m_sprite->release(); // ref = 1; // m_sprite will not be destroyed, because the reference count is 1. It will be destroyed when yourLayer is destroyed.
CCSprite* m_sprite = CCSprite::spriteWithFile(...); //ref=1; and have an autorelease property 
yourLayer->addChild(m_prite); // ref = 2; // although here the reference count is 2, 
//it will be subtracted by 1 at the end of current frame because it's an autorelease object,
//its value will be 1 at the next frame. So when the layer is destroyed, it can be release by its parent.

Rubby Entertainment wrote:

Sorry for ask so much.
Should I call release or autorelease?
I think I can call autorelease and It will be destroy auto after I have nothing use it,right?
If i only call release, the CCPoolManager don’t add it, and it can’t destroyed auto?

Clear. Thank you very much.