CreateJS / EaselJS

The Easel Javascript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier.
http://createjs.com/
MIT License
8.11k stars 1.97k forks source link

Memory leak when tween assigned #1063

Open DarkMatters opened 3 years ago

DarkMatters commented 3 years ago

I discovered a memory leak when removing children that have a tween on them.

in my case, a child has a tween looping forever. when the parent is destructed, the tween is never freed.

   var sprite= new new createjs.Sprite(sheet, frame);
   var child = new createjs.Shape(graphics);
   createjs.Tween.get(child, { loop: -1 })
                .to({ alpha: 0 }, 1000, createjs.Ease.getElasticIn(2, 5))
                .to({ alpha: 0.5 }, 1000, createjs.Ease.getElasticIn(2, 5));

then when I'm done with the sprite:

   sprite.removeAllChildren();
   stage.removeChild(sprite);

this causes a memory leak. while the solution isn't hard, you can manage resources yourself

   var child = sprite.getChildAt(index);
   createjs.Tween.removeTweens(child);
   sprite.removeAllChildren();
   stage.removeChild(sprite);

it really seems to be a good library it should somehow do this internally, as the library is good about cleaning everything else up, and there doesn't seem to be any documentation anywhere that you need to call remove tweens when tween loop forever or if you destruct their targets before the tween completes.

I wanted to run this by everyone to see what the thoughts are on this before I started to look into it further.

danzen commented 3 years ago

Hi @DarkMatters - thanks for the report. I wonder if we would want the animation to still be going if we add the child back. When we dispose() in ZIM we recursively go through children and remove animations. But when we remove the child, we do not stop the animations. I do not think there is a dispose in CreateJS so perhaps it is up to the developer to manually remove tweens if they are not going to use them again. I agree, that is tricky to remember and probably does seem like it would be a given. It could be added to the docs at least as an interim solution. Anyone else have any thoughts? Cheers.