简体   繁体   中英

Should i call dispose() texture when create another texture in Libgdx?

I'm creating a game with Libgdx. My game will load a image and show on screen like this:

sprite = new Sprite(texture1);

And then it will load another image in few seconds and show it.

sprite = new Sprite(texture2);

So, should I call spirte.getTexture().dispose() before I load the second image? Or when we create new Sprite, the fist Texture will be dispose automatically?

Yes, it must be called on any disposable object before you lose your reference to it or there will be a memory leak.

If you are going to use a Texture for the lifetime of your game, you might not be planning to ever need to dispose it, but on Android you still have to in the Game's dispose method because there are cases where Android will shut down your Activity but not the entire Application so when the user reopens your game, all the previous textures are leaked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM