ReflectionPool - static reference loss on Android?

Anything libgdx related goes here!

Re: ReflectionPool - static reference loss on Android?

Postby tomski » Wed Apr 17, 2019 2:22 pm

Sure, but no one is going to test your code when you include some unrelated third party library in order to try to reproduce a bug that you think is just core LibGDX.

If you can provide a minimal example, with very small amounts of code that would show the usecase and easily runnable (which should be pretty trivial), then we can comment on the issue and push something into master so you can have control over these pools at runtime. But so far we hvaen't a solid example of where this problem is coming from or why the person is pooling such problematic objects in the first place.

There are plenty of objects that you can put in these pools that will be absolutely fine to outlive your application, and more than fine on pause/resume.
tomski
 
Posts: 865
Joined: Wed Jun 26, 2013 6:06 pm

Re: ReflectionPool - static reference loss on Android?

Postby evilentity » Wed Apr 17, 2019 2:28 pm

Visui uses static resources, it must be disposed when app is disposed.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4727
Joined: Wed Aug 24, 2011 11:37 am

Re: ReflectionPool - static reference loss on Android?

Postby graham01 » Wed Apr 17, 2019 2:41 pm

Disposing VisUI does not make a difference.

How about this?

Code: Select all
public class LeakyPoolsTest extends ApplicationAdapter {

    public static class TestObject {
        int instance;

        public TestObject(){
            instance = LeakyPoolsTest.instance;
        }
    }

    private static int instance;

    private TestObject object1, object2;

    @Override
    public void create() {
        super.create();
        instance = MathUtils.random(0, 999999);
        object1 = Pools.obtain(TestObject.class);
        object2 = Pools.obtain(TestObject.class);

        System.out.println("this instance: "+instance);
        System.out.println("TestObject1 instance: "+object1.instance);
        System.out.println("TestObject2 instance: "+object2.instance);
    }

    public void dispose(){
        Pools.free(object1);
        Pools.free(object2);
    }
}



Is there a specific reason for not clearing the pools? My point is this - as it currently stands, people need to be careful not to use any static references within objects that they intend to get from a reflection pool. If they are unaware of this trait of the android lifecycle, and they are unaware reflection pools are statically stored and not cleared during the lifecycle, how are people supposed to know this? I know I'm not the brightest badger in the world, but I'm sure there will be a few others who are not aware of these things, and if they pool objects that get any dynamic data from the app statically (which they have to because it must be a default constructor), it is going to lead to very frustrating bugs.

I'm not looking for any beef, if I am misunderstanding something here then please do fix me
graham01
 
Posts: 202
Joined: Thu Jul 09, 2015 4:53 am

Re: ReflectionPool - static reference loss on Android?

Postby evilentity » Wed Apr 17, 2019 2:46 pm

You need to be careful when you are using any statics on android. This is not specific to pools.
On other platforms jvm goes away when you restart the app so you dont have to think about statics a whole lot. On android its not always the case, so you do have to think about them.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4727
Joined: Wed Aug 24, 2011 11:37 am

Re: ReflectionPool - static reference loss on Android?

Postby tomski » Wed Apr 17, 2019 2:48 pm

There is no beef, and im happy to improve the library whereever possible, its just good to have concrete examples that are usiung good practice to demonstrate that we need this.

You are creating a local reference of a static variable in your object. Thats not generally the best idea because you are never reinitailizing the field when the LeakyPoolsTest.instance is reinitizlied. Why are you not just referencing that global static directly? This does not showcase an issue with Pools, Pools just demonstrates a problem in your method here.

As to why they are not cleared, its not required would be my guess. Its been that way for a while, it doesn't mean its 100% right, but you can see by previous issues that its very minimal use cases that people seem to have a problem with it. Typically that suggests people are doing something wrong.

Btw, Dispose is never gaurenteed to be called, you cannot rely on a clear on dispose itself to resolve this problem. You would have to clear on recreate also.
tomski
 
Posts: 865
Joined: Wed Jun 26, 2013 6:06 pm

Re: ReflectionPool - static reference loss on Android?

Postby graham01 » Wed Apr 17, 2019 3:04 pm

I find statics much cleaner, much like Gdx.*, I have MyProject.* with all my common objects static, and much like Gdx I fix these static references in resume(). I believe it should be fine to create local references to any of my static objects in MyProject.*, because (like Gdx.*) I assume that they are essentially final. This is totally fine because generally all objects holding these references will be recreated on launch, the only exception is the objects in these pools.
graham01
 
Posts: 202
Joined: Thu Jul 09, 2015 4:53 am

Re: ReflectionPool - static reference loss on Android?

Postby tomski » Wed Apr 17, 2019 3:21 pm

graham01 wrote:... I assume that they are essentially final...


This is the issue then, because they aren't final. Why do you have local references to these no final global statics?
tomski
 
Posts: 865
Joined: Wed Jun 26, 2013 6:06 pm

Re: ReflectionPool - static reference loss on Android?

Postby shatterblast » Wed Apr 17, 2019 3:34 pm

graham01 wrote:I find statics much cleaner, much like Gdx.*, I have MyProject.* with all my common objects static, and much like Gdx I fix these static references in resume(). I believe it should be fine to create local references to any of my static objects in MyProject.*, because (like Gdx.*) I assume that they are essentially final. This is totally fine because generally all objects holding these references will be recreated on launch, the only exception is the objects in these pools.


Why not try the Lombok library? It seems like a good idea so far with my endeavors in learning Spring. I can tell you that my own code has MANY "getters and setters", so I will likely integrate it into my own project in the future. It does not exactly replace the convenience of static variables, but I think there are decent ways around that. If I correctly understand its process, it only works during compile time, so it will not affect the performance of your game. I had to specifically get a plug-in installed in IntelliJ, though, just to get it working. Otherwise, a few Spring projects would not compile without it. However, Gradle from the command-line seems to naturally understand it just fine. Otherwise without the plug-in, a bunch of methods appear missing in IntelliJ.

https://projectlombok.org/
Last edited by shatterblast on Wed Apr 17, 2019 3:57 pm, edited 7 times in total.
shatterblast
 
Posts: 230
Joined: Sun Jul 06, 2014 1:14 pm

Re: ReflectionPool - static reference loss on Android?

Postby graham01 » Wed Apr 17, 2019 3:45 pm

I assume they are created on app launch and never changed, other than relinked in resume(), but local references are still fine because the objects themselves are not reinitialized. I make local references just for ease of coding, lets see I need to access MyProject.object1.object2.object3.method() a bunch of times, it's much easier to just create a local reference. I guess I just need to rethink my strategy then, but the only case this has been a problem is with the lingering pooled objects. If everyone else already knows not to do this then I guess I am wrong and there is no need for libgdx to clear this pool. I am a totally self taught programmer and have definitely missed on some vital lessons :D
graham01
 
Posts: 202
Joined: Thu Jul 09, 2015 4:53 am

Re: ReflectionPool - static reference loss on Android?

Postby evilentity » Wed Apr 17, 2019 4:30 pm

Statics in Gdx.* are just references that are reset in each resume, so they dont suffer from statics not being reset problem. Unless you store the ref somewhere yourself. Stuff like Gdx.app != app is sometimes used to detect potential change. As was said before, this is android quirk with statics. Gotta deal with it one way or another.

As an aside, lombok is pretty cool!
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4727
Joined: Wed Aug 24, 2011 11:37 am

PreviousNext

Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest