In a game I'm writing using LibGdx, I'm wondering if a memory leak will occur on Android when the home key is pressed or a phone call gets received and the game gets put into pause.
I have a Singleton class which holds all of my games assets and I use a static method to get access to it.
public class Assets
{
private static Assets assetsInstance;
private Assets()
{
//load assets....
}
public static Assets getInstance()
{
if (assetsInstance == null)
{
assetsInstance = new Assets();
}
return assetsInstance;
}
public void nullAssets()
{
//here things are set to null
assetsInstance = null;
}
}
In my Main class:
public class Main extends ApplicationAdapter
{
public static Assets gameAssets;
private GameStateManager gsm;
public void create()
{
gameAssets = Assets.getInstance();
gsm = new GameStateManager(this);
}
public void pause()
{
gameAssets.nullAssets();
gameAssts = null;
}
}
My GameStateManager looks like this:
private int currentState;
private Main m;
private GameState[] gameStates;
private final in NUMBEROFSTATES = 6;
public static final int TITLESTATE = 0;
public static final int PLAYSTATE = 1;
//more states
public GameStateManager(Main m)
{
this.m = m;
gameStates = new GameState[NUMBEROFSTATES];
currentState = TITLESTATE;
loadState(currentState);
}
My GameState class:
public abstract class GameState
{
protected GameStateManager gsm;
protected Main m;
protected GameState(GameStateManager gsm)
{
this.gsm = gsm;
m = gsm.m;
}
//abstract methods
}
The game states all extend GameState.
By calling pause() in the Main class which sets gameAssets
to null and also assigning assetsInstance
to null, does that take care of everything needed to let the garbage collector remove the singleton? Or does the fact that GameState class has m = gsm.m;
mean something will remain in the memory and become a problem?