ReflectionPool - static reference loss on Android?

Anything libgdx related goes here!

Re: ReflectionPool - static reference loss on Android?

Postby graham01 » Wed Apr 17, 2019 4:57 pm

Yea, so after playing with this some more, it seems like this is a problem with object initialization that I don't currently know how to get around cleanly. Take the following for example, as this is much more relevant to my problem;

Code: Select all
    public static class TestObject {
        final Image image;

        public TestObject(){
            image = new Image(LeakyPoolsTest.texture);
            image.setPosition(100, 100);
        }
    }

    private Stage stage;

    private static Texture texture;

    private TestObject object1;

    @Override
    public void create() {
        super.create();

        stage = new Stage();

        Pixmap pixmap = new Pixmap(100, 100, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.WHITE);
        pixmap.fill();

        texture = new Texture(pixmap);
        pixmap.dispose();

        object1 = Pools.obtain(TestObject.class);

        stage.addActor(object1.image);

    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act();
        stage.draw();
    }

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


Okay, static Texture = bad. But it is created in create() and disposed in dispose(), so it shouldn't be a problem right? If I want to initialize anything in a pooled object that uses data outside the object, it must be fetched statically as it has to be initialized in a default constructor. Now I could have a set() method in my TestObject, that will re-apply the correct texture, and then I could do;

Code: Select all
TestObject object = Pools.obtain(TestObject.class);
object.set();


But I feel it's cleaner if objects manage their own state. What I think is missing to be honest if libgdx is happy to reuse objects from previous instances, is a Poolable.obtained() method, much like the Poolable.reset(), so at least any broken references can be reformed if necessary.
graham01
 
Posts: 202
Joined: Thu Jul 09, 2015 4:53 am

Re: ReflectionPool - static reference loss on Android?

Postby evilentity » Wed Apr 17, 2019 5:36 pm

Clear the pool in create() and pretend the past didnt happen.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4727
Joined: Wed Aug 24, 2011 11:37 am

Previous

Return to Libgdx

Who is online

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