user1393319
user1393319

Reputation: 41

LibGDX setScreen() crashes game when done multiple times

I have a GameScreen and after end of the level I set the screen back to GameScreen as a restart when user taps restart button. I do this this.setScreen(new GameScreen(game)); and before I do that line of code I dispose the screen itself, all the textures used in screen, font files and so on, everything except box2D (world because disposing it gives me native error and makes the game crash ). But even though I dispose assets before I set screen game still crashes after 15-20 restarts.

I have analyzed the memory usage by printing JavaHeap, and found out that the momory usage increases in every restart until certain point and then gets back to low point like this:

- Restart1: 10MB
- Restart2: 13MB
- Restart3: 15MB
- Restart4: 10MB
- Restart5: 11MB
- Restart6: 14MB
- Restart7: 9MB

I have read about memory usage and found out that this kind of behavior is normal. But still my game crashes after few restarts without even giving error message.

What could be causing this?

EDIT: I tested the game on ZTE Blade and found out that the game gets slower by every reset, but still crashes in around 15-20 resets.

Upvotes: 1

Views: 1276

Answers (2)

ozgeneral
ozgeneral

Reputation: 6779

You probably have solved your problem somehow since it has been almost a year but I will just put this here anyway, hopefully it may help people who are looking for solutions to this kind of errors.

I have a similar mechanism for one of my applications and I experienced a native crash with Box2d world too. It was when I used setScreen(new GameScreen(game)) after disposing the original one.

I used to have the following kind of initialization:

public class GameScreen implements Screen {
/*Global Variables*/
... 
final private static World world = new World(new Vector2(0, 0), true);  //Create a world with no gravity;
...
public GameScreen(SGM game){...}

It turned out that I have to initialize world in the constructor. Now I have the following working without any crash no matter how many times I dispose and recreate it:

public class GameScreen implements Screen {
/*Global Variables*/
... 
final private static World world;
...
public MatchScreen(SGM game){
this.game = game;
world = new World(new Vector2(0, 0), true);   //Create a world with no gravity

I'm not sure if that was also the cause in your case but well, it's just another suggestion.

Upvotes: 0

Chase
Chase

Reputation: 3183

The memory up and down pattern is standard for garbage collection, you only have to worry if it starts being unable to reach the previous low point after a garbage collection as that would indicate a memory leak. It sounds like there might be something you aren't disposing of but why are you disposing anything if you are just going to reload all the same assets?

Switch to using the AssetManager. If you call AssetManager.load in your Screen constructor, AssetManager.finishLoading in your Screen.show method and AssetManager.unload in your Screen.hide method you should never unload any of your GameScreen assets because of how AssetManager does reference counting and you would only unload those assets if you navigated to a different screen. Don't forget to call `AssetManager.update in your render method.

Upvotes: 1

Related Questions