Jump to content

Correct way of deleting a Display Object

Fricken Hamster

Recommended Posts

Hey, I couldn't find any good references for this, but what is the correct way of deleting a display object?

It seems the destroy function only unsets all the fields in a Display object, so currently I am doing

1. removing from parent display object

2. running destroy

3. removing references to the display object.

This seems to function without any errors, and shouldn't leave any references left. 

Link to comment
Share on other sites

Nice nickname, I like it :)

Do you know how Javascript Garbage Collector works? Object is removed when there is no path of references from root context to it. If you have some array that contains all your displayObjects, and you dont remove it from there, it wont be freed, you'll need 3). In most cases, you dont need to think about that, just use 1). I doubt you can make memory leak with a plain PIXI.Container or PIXI.DisplayObject.

If object is a Text or Mesh , then we have a problem: that objects have links to WebGL stuff which is not managed by javascript and eats videomemory. That's why we have "destroy()" method. For Texts and Meshes do 2).


If you loaded texture with standart way, like fromImage or with PIXI.loader, you dont have to destroy() it, because its texture is cached and will be reused for other sprites you create from the same file. In that case destroy() will affect your performance in a bad way.

If you created Sprite with texture that was created from canvas which you obtained dynamically, call destroy(), you wont need that texture anymore. 

Link to comment
Share on other sites

Thanks for the response

For clarification,
1,2 and 3 are not separate.
I am doing all 3 in that order.

Not removing children and running destroy seems to give me errors. I don't believe destroy makes any changes on the parent container unless, the container checks to see if it's children have been destroyed when it is trying to render, which I don't believe it does because of the errors I get running destroy without first removing the display object from the container.

Link to comment
Share on other sites

Its all about parameters. As you see, if you call destroy(true) you wont be able to use that texture anymore, and if you loaded it from atlas, that will be a big problem. If you call destroy(true, true) then it will purge the image from videomemory. Be careful of what are you doing.

Sprite.prototype.destroy = function (destroyTexture, destroyBaseTexture)

    this.anchor = null;

    if (destroyTexture)

    this._texture = null;
    this.shader = null;


Link to comment
Share on other sites

As per the documentation, I am not adding any parameters to the destroy method, to not destroy the textures.

However, I guess what I am really wondering is if all 3 steps are necessary for just deleting a Sprite, though I guess it is only steps 1 and 2 that could maybe make sense to be combined in some fashion. It might have been this way in flash, but thats too long ago for me to remember :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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