Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assets MUST NOT be loaded in static code #42

Open
andidevi opened this issue Jan 31, 2018 · 2 comments
Open

assets MUST NOT be loaded in static code #42

andidevi opened this issue Jan 31, 2018 · 2 comments

Comments

@andidevi
Copy link
Contributor

There are issues when loading asses in static code as that is exposed to a race condition. Loading assets with SkinLoader must not happen before class Klooni is initiated. In other cases Gdx.graphics will be null. It happens that this condition is accidently met for the current state of the code.

The order classes are loaded and initialized is subject to the class loader involved and that may change in the future, especially as this is an Android app and Google is experimenting much with it's runtime.

There are more reason to not load assets in static code, ie. error handling and the like.

Example of issues (initiate effects in static code from class Klooni):
SkinLoader static
Exception in thread "main" java.lang.ExceptionInInitializerError
at io.github.lonamiwebs.klooni.effects.WaterdropEffectFactory.(WaterdropEffectFactory.java:38)
at io.github.lonamiwebs.klooni.Klooni.(Klooni.java:46)
at io.github.lonamiwebs.klooni.desktop.DesktopLauncher.main(DesktopLauncher.java:34)
Caused by: java.lang.NullPointerException
at io.github.lonamiwebs.klooni.SkinLoader.(SkinLoader.java:43)
... 3 more

@Lonami
Copy link
Member

Lonami commented Feb 5, 2018

Is this still an issue after #43 got merged? (I see you moved the Waterdrop static initialization into the constructor which is nice).

Also regarding the FIXME you introduced:

// FIXME theme should NOT be static as it might load textures which will expose it to the race condition iff GDX got initialized before or not
public static Theme theme;

Sure, it's static, but it's not loading anything. It defaults to null, and is loaded later on create:

public void create() {
onDesktop = Gdx.app.getType().equals(Application.ApplicationType.Desktop);
prefs = Gdx.app.getPreferences("io.github.lonamiwebs.klooni.game");
// Load the best match for the skin (depending on the device screen dimensions)
skin = SkinLoader.loadSkin();
// Use only one instance for the theme, so anyone using it uses the most up-to-date
Theme.skin = skin; // Not the best idea
final String themeName = prefs.getString("themeName", "default");
if (Theme.exists(themeName))
theme = Theme.getTheme(themeName);
else
theme = Theme.getTheme("default");

So, is this still wrong?

@andidevi
Copy link
Contributor Author

The text is according to your TODO that have been there before. I did'n have any look into it beside the code of SkinLoader.

As to to the comment in Line 89 says it is a singleton, you should not allow public write access to it and use a public getter ... and have a look if that's really a performance hit or not ;)

As it isn't loaded that early there's no need for static at all. If other classes need access, they should simply know Klooni. This would lead to more refactoring, ie. making the cellTexture a parameter of Cell.draw or supplying it at cell creation and changing the parameter of set to the already resolved texture to having Cells and maybe other to not needing to know Klooni.

Alternatively you could have a private static variable to store the Klooni instance and supply it with a public static getter.

... much changes and I don't see a practical benefit now. As a big difference to effects you only have one Theme class and there's no need to change as long as that one size fits all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@Lonami @andidevi and others