stauzs Posted April 2, 2014 Share Posted April 2, 2014 Hi, while working on a project I found nasty memory leak - after few hours of debugging I finally managed to locate problem. here is test case to reproduce: function main(){ var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: function(){ this.game.load.spritesheet('test', 'dude.png', 32, 48); }, create: function(){ //testLeak(); testLeak(true); } }); var info = document.createElement("span"); document.body.appendChild(info); var count = 0; var increaseCount = function(){ count++; info.innerHTML = count; }; var frames = [0,1,2,3]; var testLeak = function(addAnimation){ var sprite = game.world.create(0, 0, "test"); if(addAnimation){ sprite.animations.add("default", frames, 10, false); } increaseCount(); window.setTimeout(function(){ sprite.destroy(true); testLeak(addAnimation); },0); };} debug.zip Problem is that sprite animations isn't destroyed properly. Can anyone suggest workaround? Is it possible to reuse animations e. g.//......var animation = sprite.animations.add("default", frames, 10, false);//......sprite = game.world.create(0, 0, "test");sprite.addAnimation(animation); cheers,stauzs Link to comment Share on other sites More sharing options...
drhayes Posted April 2, 2014 Share Posted April 2, 2014 It might be because I'm wearing my dumb hat this morning, but what use case do you have for re-using animations on a sprite you've destroyed? If you're destroying the sprite only to make another one exactly like it you probably shouldn't have destroyed it in the first place. Unless I'm missing something specific to your case? I'm asking because you might really mean "kill" so that you can re-use it. That way, there's no memory leak (at least as far as your code is concerned). Although "destroy" should probably "release" (as much as you can in JS anyway) the animations. Heppell08 and stauzs 2 Link to comment Share on other sites More sharing options...
stauzs Posted April 2, 2014 Author Share Posted April 2, 2014 Reuse animation of destroyed sprite seemed to me like one of possible workarounds - as sprite are destroyed correctly, as long as it don't use animations. Destroying sprite seemed more appropriate for my case - as I don't know - will I use this sprite again or not ( obviously - in most cases I will use it ) However by just "killing" it - it will definitely stay in memory - so I will need to destroy it eventually. I will adjust code for sprite re-use by killing and reviving - it should significantly reduce memory usage and pressure on GC, but it won't solve problem in general. thanks for tip Link to comment Share on other sites More sharing options...
stauzs Posted April 2, 2014 Author Share Posted April 2, 2014 OK - found the leak - at Animation.Manager destroy method ( http://docs.phaser.io/AnimationManager.js.html#sunlight-1-line-313 ) resets _anims object - but signals onPause / onResume from animation itself - keeps living - and makes a memory leak atm using this workaround ( tested with 3M sprites - so far so good ):var destroySprite = function(sprite){ var anims = sprite.animations._anims; var anim = null; for(var i in anims){ anim = anims[i]; //http://docs.phaser.io/Animation.js.html#sunlight-1-line-380 anim._parent = null; anim._frames = null; anim._frameData = null; anim.currentFrame = null; anim.isPlaying = false; anim.onStart.dispose(); anim.onLoop.dispose(); anim.onComplete.dispose(); anim.game.onPause.remove(anim.onPause, anim); anim.game.onResume.remove(anim.onResume, anim); anim.game = null; } sprite.destroy(true);};heavy pressure on GC (as expected) - but no leaks george, drhayes and rodrigogrow 3 Link to comment Share on other sites More sharing options...
rich Posted April 14, 2014 Share Posted April 14, 2014 Thanks for this - have merged it into the dev branch: https://github.com/photonstorm/phaser/commit/5d0ea6453b011b28a209cf37bc9064a7538cd0aa drhayes 1 Link to comment Share on other sites More sharing options...
Recommended Posts