canvasman Posted March 5, 2020 Share Posted March 5, 2020 (edited) I am creating multiple sprites from same texture in PIXI v5, I have few questions: 1. When I am not going to use specific sprite anymore, do I need to remove the sprite from container with container.removeChild(sprite) before calling sprite.destroy()? I call destroy without arguments because I don't want to destroy the texture which I might use later to create more sprites. 2. What is the difference between calling container.removeChild(sprite) and sprite.destroy(without arguments)? Does the GC collect everything regarding the sprite in either way? (except the texture which is in the GPU memory). Thanks! Edited March 5, 2020 by canvasman ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 5, 2020 Share Posted March 5, 2020 1. destroy() in sprites are actually a over-, javascript GC will take it anyway just like any other object. It could be different if Textures had links to sprites (events, subscribe) but they aren't. You remove sprite from container and pixi forgets about it. 2. sprite fields are getting slaughtered and its not usable anymore, many things will just throw error when you try to use them canvasman 1 Quote Link to comment Share on other sites More sharing options...
canvasman Posted March 5, 2020 Author Share Posted March 5, 2020 7 minutes ago, ivan.popelyshev said: 1. destroy() in sprites are actually a over-, javascript GC will take it anyway just like any other object. It could be different if Textures had links to sprites (events, subscribe) but they aren't. You remove sprite from container and pixi forgets about it. 2. sprite fields are getting slaughtered and its not usable anymore, many things will just throw error when you try to use them Okay! Regarding the #1 question, would you call both removeChild() and .destroy() in that order or only either in my case when you don't need to use that same sprite later? Quote Link to comment Share on other sites More sharing options...
themoonrat Posted March 5, 2020 Share Posted March 5, 2020 I'd just call destroy As part of the DisplayObject's destroy function, it has the following code, that removes itself from any parent it has if (this.parent) { this.parent.removeChild(this); } canvasman 1 Quote Link to comment Share on other sites More sharing options...
canvasman Posted March 5, 2020 Author Share Posted March 5, 2020 7 hours ago, themoonrat said: I'd just call destroy As part of the DisplayObject's destroy function, it has the following code, that removes itself from any parent it has if (this.parent) { this.parent.removeChild(this); } Moonrat is the best! Thanks dude Quote Link to comment Share on other sites More sharing options...
riccioverde11 Posted January 6, 2021 Share Posted January 6, 2021 Hello, does anyone know why removing children with .removeChildren() doesn't also trigger the .destroy() of those children? If it is by design, can anyone suggest a good pattern? Quote Link to comment Share on other sites More sharing options...
themoonrat Posted January 6, 2021 Share Posted January 6, 2021 It's by design, because I may have some children that I want to add to one parent one moment, then to another parent the next. If you went through each child, and called destroy, then that child will automatically remove itself from the parent, so maybe that's the better way to go? riccioverde11 1 Quote Link to comment Share on other sites More sharing options...
riccioverde11 Posted January 7, 2021 Share Posted January 7, 2021 (edited) Well technically that's what I'm doing now. Although, what if I have children of children I'd like to destroy? Isn't there some API ready for this? Or should I just iterate? My use case in particular is: I have 2 main scenes (which i switch based on a condition). Both scenes, have along the pixi tree - nested at an arbitrary level - some components with observable properties and on destroy of that component i remove those observers. When I switch scene the idea was to removeChildren from it to "clear" the tree and free some memory, but by doing this, I'm not actually destroying every single instance, and some observer might still be alive. What's the best approach in this case? Edited January 7, 2021 by riccioverde11 Gave more context on the issue Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted January 7, 2021 Share Posted January 7, 2021 the best approach is to 1. read source code 2. run memory profiling (take heap dump) and look whether it works for you correctly: which objects are referenced from root and how everything else - we can make advices but they dont guarantee that it will work for you Quote Link to comment Share on other sites More sharing options...
riccioverde11 Posted January 8, 2021 Share Posted January 8, 2021 Mhm, thanks, I thought there was a standard for these kind of procedures, but I guess it's left to the dev what's best for himself. Thanks for the advice. ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.