Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitmapData in Sprites is never freed from PIXI.CanvasPool when destroyed #2261

Closed
AnderbergE opened this issue Dec 17, 2015 · 4 comments
Closed

Comments

@AnderbergE
Copy link

If you create a sprite from a BitmapData like such:

var bmd = game.add.bitmapData(game.world.width, game.world.height);
bmd.ctx.fillStyle = color || '#000000';
bmd.ctx.fillRect(0, 0, game.world.width, game.world.height);
var sprite = game.add.sprite(0, 0, bmd);

When the sprite is destroyed, the BitmapData will not be removed from PIXI.CanvasPool.

In my case I change states quite often and one of them uses many BitmapData-sprites. This can easily consume 10-15mb of memory each time the state is started, which is never freed.

There is, possibly, a note about this already:
https://github.com/photonstorm/phaser/blob/master/src/pixi/display/DisplayObject.js#L687
Which is called from here:
https://github.com/photonstorm/phaser/blob/master/src/gameobjects/components/Destroy.js#L142

The BitmapData has code to remove it in its destroy function. But this function is never called.
https://github.com/photonstorm/phaser/blob/c9c85330ab60547b39ba4c9400c258ed7fb2a317/src/gameobjects/BitmapData.js#L1814

@AnderbergE AnderbergE changed the title BitmapData is never freed from PIXI.CanvasPool when destroyed BitmapData in Sprites is never freed from PIXI.CanvasPool when destroyed Dec 17, 2015
@stoneman1
Copy link
Contributor

I thought it was about the baseTextures not clearing up correctly on sprites when I found out that when I create sprites with group.create(0,0,BMD) and removing them with group.removeAll(true,true) does not actually free them. Checked with webgl inspector.. Should try to destroy those bitmapdatas also if that helps.. This should be fixed asap. :)

@stoneman1
Copy link
Contributor

what about adding this to the destroy.js?
if (this.key.type == 13) {
this.key.destroy();
}

@photonstorm
Copy link
Collaborator

I'm not sure there's a "catch all" fix for this. Take the following example:

var bmd = game.add.bitmapData(game.world.width, game.world.height);
bmd.ctx.fillStyle = color || '#000000';
bmd.ctx.fillRect(0, 0, game.world.width, game.world.height);
var sprite1 = game.add.sprite(0, 0, bmd);
var sprite2 = game.add.sprite(0, 0, bmd);
sprite1.destroy();

This would then invalidate sprite2's texture. You could say that on a State swap this wouldn't matter, but I've some games where I precalc a texture in my boot state and store it in the cache, so in those cases it would destroy the original as well.

I honestly think this needs flagging in the docs and handling in userland code.

photonstorm added a commit that referenced this issue Feb 3, 2016
…thod: `destroyTexture`. This boolean (which is false by default) controls if the BaseTexture of the Game Object should be destroyed or not. This is extremely useful in situations where you've got a lot of dynamic assets you no longer need, such as textures created from BitmapDatas. You must set the `destroyTexture` argument yourself. This can be done in a custom Game Object destroy method or as part of your state shutdown (#2261)
@photonstorm
Copy link
Collaborator

There, I'm quite happy with the solution offered in the latest commit. That, combined with the _swapCanvas change will make quite a difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants