Reputation: 5144
According to this article, a SpriteBatch
instance needs to call dispose()
once it is no longer needed. However, as I examine some of libgdx's official examples like Pax Britannica and Super Jumper, I found out that they never call SpriteBatch.dispose()
. Why is that?
Upvotes: 7
Views: 1909
Reputation: 737
I've created games where I have multiple screens which all has their own SpriteBatch. I just removed all dispose() methods of the batches and for me, there's no effect of this. So keep in mind to check for this before releasing your product. Even if you can't feel any downside not to dispose batches, there's no reason not to dispose them. Just do it on the Screen implemententations dispose methos, takes about 1 nano second to do :)
Upvotes: 0
Reputation: 19776
I think the given demo games try to keep things simple. They are supposed to show how the basic things in libgdx work in a minimalistic way and thus also abstract a little of some details. That's useful for beginners to not bloat up the examples with a lot of very specific code.
In a real world example I think SpriteBatch.dispose()
has to be called in the dispose()
method of the GameScreen
in SuperJumper for example. And also GameScreen.dispose()
has to be called when switching back to the MainMenuScreen
, because this doesn't happen automatically as well.
When creating a Spritebatch like this new SpriteBatch()
, it creates one internal Mesh
. When not calling SpriteBatch.dispose()
this one mesh would also not be disposed and thus SuperJumper has a memory leak there.
Upvotes: 1
Reputation: 2752
SpriteBatch
must always be disposed.
Internally, it creates and manages several Mesh
objects. These objects allocate vertex/index arrays on the GPU. Those are only deallocated if you explicitly call Mesh#dispose()
, which will be triggered by calling dispose()
on your SpriteBatch object.
It will also, by default, create its own ShaderProgram
. And similarly, would be leaked if you didn't call dispose()
.
If the demo's aren't doing this, perhaps it's time to send a pull request!
Upvotes: 5