Display multiple Particles at the same time, particle system leak memory and crash

Hi everyone,

I’m working on my first game using cocos2d-x and I have a problem with Particle System. I use particle system when I destroy an object in my game and I have multiple particle at the same time. My game becomes slowly and crash after some playing times. When I don’t use particle system, my game run ok without crash. I’ve tested on both Iphone and Android device and the game crashed on both of them.

In the game, I loop on my objects array and display particle for each object by this code:

CCParticleSystem* m_emitter = CCParticleExplosion::create();
m_emitter~~>retain;
parent~~>addChild(m_emitter, 4);

m_emitter~~>setTexture~~>textureForKey );
m_emitter~~>setStartSize;
m_emitter~~>setStartSizeVar(6);
m_emitter~~>setEndSize;
m_emitter~~>setEndSizeVar(12);
m_emitter~~>setTotalParticles;
m_emitter~~>setSpeed(150);
m_emitter~~>setDuration;
m_emitter~~>setGravity(ccp(0,200));
m_emitter
>setStartColor((ccColor4F){1,1,1,1});
m_emitter~~>setStartColorVar);
m_emitter~~>setEndColor((ccColor4F){1,1,1,0});
m_emitter~~>setEndColorVar);
m_emitter~~>setBlendAdditive(false);
m_emitter~~>setAutoRemoveOnFinish;
m_emitter~~>setPosition( CCPointMake(posX, posY) );

Does anyone has experience with this problem? Please help me fix it.
Thank you very much and sorry for my bad English.

Thanks,

Could you upload a sample based on HelloCpp to reproduce this issue?

Hi Chen,

Cocos2d-x forum doesn’t allow me submit zip file of test project here. In my HelloWord project, I only implement ccTouchesEnded method like this:

void HelloWorld::ccTouchesEnded(CCSet pTouches, CCEventpEvent){
for(int i=0; i< 10; i +){
int posX = 50
rand() 400;
int posY = 50 + rand() 280;

CCParticleSystem* m_emitter = CCParticleExplosion::create();
m_emitter~~>retain;
this~~>addChild(m_emitter, 1);

m_emitter~~>setTexture~~>addImage );
m_emitter~~>setStartSize;
m_emitter~~>setStartSizeVar(6);
m_emitter~~>setEndSize;
m_emitter~~>setEndSizeVar(12);
m_emitter~~>setTotalParticles;
m_emitter~~>setSpeed(150);
m_emitter~~>setDuration;
m_emitter~~>setGravity(ccp(0,200));
m_emitter
>setStartColor((ccColor4F){1,1,1,1});
m_emitter~~>setStartColorVar);
m_emitter~~>setEndColor((ccColor4F){1,1,1,0});
m_emitter~~>setEndColorVar);
m_emitter~~>setBlendAdditive(false);

m_emitter~~>setAutoRemoveOnFinish;
if
{
m_emitter~~>setPosition( CCPointMake(posX, posY) );
}
}

}

After I touch too much on the screen, it becomes slowly and force close. It’s only force close on real device. On simulator it run well.
In my game, it doesn’t have much particle as this test. When i touch on the object of the game, that object and all the same objects beside it will be destroyed and particle effects will be displayed. It crashed when I replayed the game many times.

Could you please help?

Thank you,
AnDo.

Hi,

It’s seem OK now when I use this solution:
http://www.cocos2d-iphone.org/forum/topic/177
Just pre-allocate all particles and use them during the game.

Thanks