Quillraven / SimpleKtxGame

The LibGDX simple game with Kotlin and LibKTX using an assetmanager, pool and viewport
74 stars 14 forks source link

KTX/GDX entity disposal issues #14

Closed dankxylese closed 3 years ago

dankxylese commented 3 years ago

Thanks for the great intro to ktx, but I've ran into a problem with the newer versions of ktx/gdx with regards to entity disposal. When exiting the game, game crashes as it fails to clean it up when calling context.dispose() - thats (Game.kt:43):

E/AndroidRuntime: FATAL EXCEPTION: GLThread 595 Process: com.libktx.game, PID: 18753 java.lang.StackOverflowError: stack size 1038KB ... at ktx.inject.SingletonProvider.dispose(inject.kt:234) at ktx.inject.Context.dispose(inject.kt:200) at com.libktx.game.Game.dispose(Game.kt:43) at ktx.inject.SingletonProvider.dispose(inject.kt:234) at ktx.inject.Context.dispose(inject.kt:200) at com.libktx.game.Game.dispose(Game.kt:43) I/chatty: uid=10086(com.libktx.game) GLThread 595 identical 14 lines ...

To recreate, just clone your project and bump the ktx/gdx version to latest: gdxVersion = '1.9.11' ktxVersion = '1.9.11-b2'

Would be great to know why its doing this since I have no clue and just reverted to older versions.

Otherwise its a great guide and has tought me a lot. Thanks again.

Quillraven commented 3 years ago

I will have a look on the weekend. Thanks for reporting it.

Latest LibGDX version should be 1.9.12 currently with ktx 1.9.12-SNAPSHOT.

dankxylese commented 3 years ago

I didn't consider a snapshot to be something stable enough to use, but even bumping the version to that results in the same kind of crash.

Quillraven commented 3 years ago

@czyzby created an issue for an official 1.9.12-b1 version but I guess there was no time yet. But believe me, at least that Snapshot is not dangerous. It is in fact also just increasing the used LibGDX version to 1.9.12 and since the tests are passing you can trust it ;) Also, I verified it in two of my projects where I extensively use a lot of KTX extensions.

I already thought that the version change will not help but thank you for trying it out.

I will update the wiki and code to the currently latest versions of LibGDX and LibKTX on the weekend.

Maybe I add something twice to the context by accident or something else gets disposed twice now.

Anyway, pretty sure that I can solve it ;) I let you know when it is done.

Quillraven commented 3 years ago

@dankxylese sorry, one more thing. Can you try removing line 23 in Game.kt? I don't remember why I add the game instance itself to the context but if I am not wrong this will create an endless loop in dispose because if the game is part of the context its dispose function gets called which again disposes the context....

You also need to pass then the game instance via this instead of inject() to the screens.

This will end up in a stackoverflow. Sorry, I currently only have my phone and cannot try it out myself.

czyzby commented 3 years ago

@czyzby created an issue for an official 1.9.12-b1 version but I guess there was no time yet.

It's not as much about time as Maven Central credentials. My home laptop is still being repaired, unfortunately. I do have a backup, but I'd rather not restore it on my work laptop. I'll try to upload the next version within the next 2 weeks.

czyzby commented 3 years ago

Actually, I might just be able to access the keys. I'll let you know in the related issue if I manage to make a release sooner. Thanks for the reminder, @Quillraven.

EDIT: KTX 1.9.12-b1 was released a few minutes ago. Sorry for the delay.

Quillraven commented 3 years ago

@dankxylese I updated all branches now to Gradle 6.7.1, LibGDX 1.9.12 and LibKTX 1.9.12-b1. Also, updated the wiki in the "Setup", "Inject" and "Ashley" section.

The bug you described is fixed and it was the thing I mentioned above. I have no idea why that worked with older LibKTX version. Maybe there was a bug in inject module or KtxGame was not implementing the Disposable interface. Anyway, removing it from the context was the solution (see above).

@czyzby : thank you for the new version! It is now used in this codebase.

czyzby commented 3 years ago

Maybe there was a bug in inject module or KtxGame was not implementing the Disposable interface. Anyway, removing it from the context was the solution (see above).

That's was it, KtxGame implements Disposable since 1.9.10-b7. You might say that it worked before because of a bug - KtxGame had the dispose method, but did not implement the associated interface, which lead to it being ignored by Disposable utilities and managers. Since now it works correctly and you tried to dispose the instance in its own dispose method, this lead to the overflow.

czyzby commented 3 years ago

See https://github.com/libktx/ktx/issues/309.

czyzby commented 3 years ago

Hey @Quillraven, I'll hijack the issue a little bit. :) Just wanted to let you know that KTX now has a GitHub Discussions board. I think they either opened up the beta for all public repositories, or we qualified for that recently, because I don't remember this being available until recently.

I want the Discussions to be much less formal than issues, and not necessarily focused on questions or feature requests. I think showcasing your KTX projects is a good idea - they'll be easier to find for our new KTX users and might hopefully spark some interesting discussions. Let me know what you think!

Quillraven commented 3 years ago

@czyzby looks good to me! I was always missing that feature on github because when you simply want to ask a question or discuss something you always have to open an issue which is not correct in those cases.

Do you want me to add something? Like a "Show and Tell" topic for the things I made including:

czyzby commented 3 years ago

@Quillraven Please do! All of these sound good.