Can a SpriteBatch instance call dispose () if it's no longer in use?

According to this article, a SpriteBatch instance should call dispose() as soon as it is no longer needed. However, while studying some of the official libgdx examples, such as Pax Britannica and Super Jumper, I learned that they never called SpriteBatch.dispose() . Why is this?

+7
libgdx
source share
3 answers

SpriteBatch should always be deleted.

Inside, he creates and manages several Mesh objects. These objects allocate arrays of vertices / indices on the GPU. They are freed if you explicitly call Mesh#dispose() , which will be called by calling dispose() in your SpriteBatch object.

He will also create his own ShaderProgram . And likewise, there will be a leak if you did not dispose() .

If the demo does not do this, it may be time to send a transfer request!

+5
source share

I think these demos try to keep things simple. They should show how the basic things in libgdx work in a minimalistic way, and thus, some details are also partially abstracted. This is useful for beginners not to inflate examples with a lot of special code.

In a real world example, I think that SpriteBatch.dispose() should be called in the dispose() method of the GameScreen in SuperJumper, for example. And also GameScreen.dispose() needs to be called when returning to MainMenuScreen , because this also does not happen automatically.

When creating a Spritebatch similar to this new SpriteBatch() , it creates one internal Mesh . When SpriteBatch.dispose() is not called, this mesh will also not be removed, and thus the SuperJumper has a memory leak.

+1
source share

I created games in which I have several screens, all of which have their own SpriteBatch. I just deleted all the dispose () methods from the parties, and for me this did not affect. Therefore, be sure to check this out before releasing your product. Even if you cannot feel the lack of not to sell the parties, there is no reason not to dispose of them. Just do it on the screen to implement the methods, it takes about 1 nano-second :)

0
source share

All Articles