Particle Effect Memory Issue

  • MoseHas
    Likes 0

    Problem Description

    Hi there.

    I just found out that my particle effect will cause memory waste issue. I have tested in different ways. Would you check this for me?

    void showStarParticleEffect(const int color,const Point position, Node* node)
    {
    	ParticleExplosion* effect = ParticleExplosion::create();
    	effect->setTexture(Director::getInstance()->getTextureCache()->addImage("star.png"));
    	effect->setTotalParticles(800);
    	effect->setStartColor(getColor4F(color));
    	effect->setStartColorVar(Color4F(0, 0, 0, 1));
    	effect->setEndColor(getColor4F(color));
    	effect->setEndColorVar(Color4F(0, 0, 0, 1));
    
    	effect->setStartSize(25.0f);
    	effect->setGravity(Point(0, -300));
    	effect->setLife(1.0f);
    	effect->setSpeed(350);
    	effect->setSpeedVar(20);
    
    	effect->setPosition(position);
    	node->addChild(effect);
    	//log("add effect");
    }

     

    I remember that create() will autorelease unused objects. I do not know why it keeps stealing my memory. Original memory usage was 65K and after I broke through Level 9 it became 200K and even more.

    Thank you.

  • Sonar Systems admin
    Likes 0

    What do you mean Level 9?

  • MoseHas
    Likes 0

    Sorry I did not mention that this is Popstar game.

    All in all, everytime I eliminate stars and produce particle effect, memory usage increases. That is why I said Level 9 (from Level 1 to 9, hundreds of stars were eliminated.)

  • Sonar Systems admin
    Likes 0

    after the effect has finished trying removing it like so

     

    node->removeChild(effect);


    This reply has been verified.
  • MoseHas
    Likes 0

    Now here is my question. How do I control the effect and remove it after a period of time?

  • Sonar Systems admin
    Likes 0

    After each levels perhaps delete all effects?


    This reply has been verified.
  • MoseHas
    Likes 0

    I think it will be a little bit difficult.

    This is my structure of Popstar,

     

    The particle effect is in StarMatrix.cpp, and I call the function whenever I detect a group of stars with same color. In other words, if 5 stars is eliminated, particle effect runs 5 times in a 10 x 10 for-loop due to my width and height of the matrix. The function type is void, and I create the particle effect in StarParticle.cpp, which means I cannot delete the particles in StarMatrix.cpp, And I tried to delete particles in StarParticle.cpp, either particles were deleted immediately and nothing showed up or I failed to remove the particles.

     

    If I make you confuse I apologize for that. I have no idea how to explain it any more specificly.

  • MoseHas
    Likes 0

    Will this cause memory issue?

    //#StarMatrix.cpp
    
    	...
    	
    	for(int i = 0; i < 10; i++)
    		for(int j = 0; j < 10; j++)
    			createParticleEffect(color, position, this);
    	...
    }
    //#StarParticle.cpp
    
    void createParticleEffect(const int color, const Point position, Node* node)
    {
    	ParticleExplosion* effect = ParticleExplosion::create();
    	effect->setTexture(Director::getInstance()->getTextureCache()->addImage("star.png"));
    	effect->setTotalParticles(800);
    	effect->setStartColor(getColor4F(color));
    	effect->setStartColorVar(Color4F(0, 0, 0, 1));
    	effect->setEndColor(getColor4F(color));
    	effect->setEndColorVar(Color4F(0, 0, 0, 1));
    
    	effect->setStartSize(25.0f);
    	effect->setGravity(Point(0, -300));
    	effect->setLife(0.3f);
    	effect->setSpeed(350);
    	effect->setSpeedVar(20);
    
    	effect->setPosition(position);
    	node->addChild(effect);
    }

     

    I am thinking if I use a double for-loop to create multiple particle effects, the pointer of later-created particle will overwrite the former one. I guess this might cause memory leak.

  • MoseHas
    Likes 0

    After 3 days debugging and struggling, I have succeeded deleting these leaked memory.

    I assume that everytime we produce the particle effect without assigning pointers to it will cause memory leak. Therefore, I create an array to store the pointers.

    ParticleExplosion* effect[100];

    I was trying to create effect[10][10] but failed, so I had no choice but use one dimension array and calculate the index by myself.

    Then I use CallFunc::create to make this->removeChild(effect) a action. Finally I create a sequence to run DelayTime and CallFunc.

     

    I hope there will not be too many obstacles before I finish the game.

  • Sonar Systems admin
    Likes 0

    Great to hear :D


    This reply has been verified.

Login to reply