Jump to content

Memory leak in extended sprite


Hadik
 Share

Recommended Posts

Hello, I have simple exteneded object from sprite.

 

Its simple smoke effect with tween alpha for some effects, after tween ends, call this.destroy(), for destroy and remove this sprite from game. But somethink vierd happends.

I call this every tick, but memory increase more and more, and after few minutes browser crash. When I dont use this object, game run OK and memory not increase.

I think there must be somethink, and this think not allow GC clear this instance. But I try clear object as example, only create sprite nothink else, and on the end put this.destroy(), same memory leak.

 

But when I create only sprite (without extending and creating instance), and call destory() on this sprite, memory leaks gone.. Everythink OK.

So where is problem ? I also try after destroy set to all properties in this instance to null, same situation, GC not clear memory.

 

Problematic code here:

/* global Phaser, Utils, config */
    /**
     *  XP point body
     *
     *  @class XpPoint
     *  @extends Phaser.Sprite
     *  @constructor
     */
    SmokeEffect = function (game, x, y, black, lessTransparent, settings) {
        /**
         * @property {Phaser} game - Main game context
         * @private
         */
        Phaser.Sprite.call(this, game, x, y, 'smoke');
        game.add.existing(this);

        game.physics.enable(this, Phaser.Physics.ARCADE);
        
        //2500
        game.time.events.add(100, function () {
            this.destroy();
        }, this);
    };
    
    SmokeEffect.prototype = Object.create(Phaser.Sprite.prototype);
    SmokeEffect.prototype.constructor = SmokeEffect;

I try clear all properties in instance, but not help:

for(var property in this){
  this[property] = null;
  delete this[property];
}

When I do this everythink is OK:

var effect = this.game.add.sprite(exhaustPosition.x, exhaustPosition.y, 'smoke');
        
this.game.time.events.add(2500, function () {
   this.destroy();
}, effect);

This anoying memory leak blocking release our game, almost one year of development stop on this memory leak, anybody know how solve it ?

Link to comment
Share on other sites

In another object extends from sprite, call in update

new SmokeEffect(this.game, exhaustPosition.x, exhaustPosition.y, this._onDirt, isMoving);

and object player is global variable, I create instance of object Player (object extends sprite)

 

We are desperate with this problem, it ruined are release plans

Link to comment
Share on other sites

It's hard to say. Make sure the second sprite (the one calling `new SmokeEffect`) isn't keeping any kind of reference to the smoke sprites after they are destroyed.

Did you try https://developers.google.com/web/tools/chrome-devtools/profile/memory-problems/memory-diagnosis?hl=en ?

Could you use a Phaser.Emitter instead? You could extend Particle for it.

Link to comment
Share on other sites

Thank you, but We are looking in dev tools a lot of hours, but we see only this. http://prntscr.com/cg5u02 (comparsion between two snapshost 30 seconds)

Emitter not solve this problem,

 

For example we have a bullet object, which is created when player press key, and destroy also with timer, same problem, if player will shoot for 30 minutes, memory will be filled and game crashed. 

Link to comment
Share on other sites

Unfortunately no, it's weird.

 

I have only one solution recyclate this objects, kill and revieve in next time is require. I read lot of articles about this problem on stackoverflow.

Developers which make games in the JS, have same problesm, where create and destroy instance over again (60 times every second), and after few minutes game crash. Most recommend use recyclating of this intances.

 

Also this solution use Rich in his examples, for example /Games/Tank use this method.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...