In my libgdx game, I have multiple screens. I am a little unsure how to properly use the dipose()
method.
In each screen there are certain variables I create then dispose in the dispose()
method. I read online that if I do not dispose, it causes memory leaks and crashes the program.
But, for some reason, whenever I dispose anything in my MenuScreen
, it causes a crash in the app (Fatal signal 11
). (The error also occurs at other random times when switching different screens). I don't know if this has anything to do with me naming my files the same names in each class.
For example, in the create()
function of each class I do this:
batch = new SpriteBatch();
shapeRenderer = new ShapeRenderer();
Then in the dispose()
function of each class I do this:
batch.dispose();
shapeRenderer.dispose();
Does keeping the same variable name in each class affect my code? Aside from this I dispose every image, sound and font manually.
Even when I do not dispose anything in my MenuScreen
class, the Fatal Signal 11
error occurs every now and then when switching screens.
Sorry for being vague but I am unsure what is causing this error. There is also an approximate 1 second delay when switching screens.
Note:
When switching screens, I do:
game.setScreen(new ScreenName(game));
while game
is just an instance of my MainClass
:
final MainClass game;
Since you are passing the instance of your main class, why do you make a new batch in each class? Just use the main class's batch