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

IllegalArgumentException when removing entity and then removing components #77

Closed
chrisapril opened this issue Oct 7, 2014 · 2 comments
Labels

Comments

@chrisapril
Copy link

Hi!

i have made a litte test and i got an IllegalArgumentException. First i remove the entity and after this i remove some components from the entity.

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {

        PooledEngine engine = new PooledEngine();

        engine.addSystem((new Issue206SystemTest()).new TestSystemA("A"));
        engine.addSystem((new Issue206SystemTest()).new TestSystemAB("AB"));

        Entity e = engine.createEntity();
        e.add(new TestComponentA());
        e.add(new TestComponentB());
        engine.addEntity(e);

        int d = 0;

        engine.update(d++);
        System.out.println(d + "=------------------------------------------");

        engine.update(d++);
        System.out.println(d + "=------------------------------------------");

        System.out.println(d + "= deleting " + e);
        engine.removeEntity(e);

        System.out.println(d + "= removing components from " + e);
        e.remove(TestComponentB.class);
        e.remove(TestComponentA.class);

        engine.update(d++);
        System.out.println(d + "=------------------------------------------");

    }

    public static class TestComponentA extends Component {
    }

    public static class TestComponentB extends Component {
    }

    public static class TestComponentC extends Component {
    }

    class TestSystemA extends IteratingSystem {

        private String name;

        public TestSystemA(String name) {
            super(Family.getFor(TestComponentA.class));
            this.name = name;
        }

        @Override
        public void processEntity(Entity e, float d) {
            System.out.println("System [" + name + "] processed: " + e);
        }

    }

    class TestSystemAB extends IteratingSystem {

        private String name;

        public TestSystemAB(String name) {
            super(Family.getFor(TestComponentA.class, TestComponentB.class));
            this.name = name;
        }

        @Override
        public void processEntity(Entity e, float d) {
            System.out.println("System [" + name + "] processed: " + e);
        }

    }

}

1=------------------------------------------
2=------------------------------------------
2= deleting com.badlogic.ashley.core.PooledEngine$PooledEntity@0
2= removing components from com.badlogic.ashley.core.PooledEngine$PooledEntity@0
Exception in thread "main" java.lang.IllegalArgumentException: object cannot be null.
at com.badlogic.ashley.core.PooledEngine$ComponentPools.free(PooledEngine.java:160)
at com.badlogic.ashley.core.PooledEngine$PooledEntity.remove(PooledEngine.java:113)
at com.krautfactory.tests.ashley.Test.main(Test.java:44)

@chrisapril chrisapril changed the title IllegalArgumentException when removing entity and then components IllegalArgumentException when removing entity and then removing components Oct 7, 2014
@SgtCoDFish
Copy link
Contributor

The exception is simple enough to fix; I've added a null check to the removeInternal function and I'll send in a PR for that.

The reason we even saw the exception seems to be a little more sinister; I'm guessing it's probably related to using a PooledEngine but not using pooled components via createComponent. This is probably related to #78. I'm investigating :)

EDIT: Found it and it's not so bad; when you remove a PooledEntity from an engine, it's reset and all the components attached to the entity are removed. That makes your attempts to remove the components after you've already removed the entity "fail". The really simple null check I added totally solves this but basically

    e.remove(TestComponentB.class);
    e.remove(TestComponentA.class);

are pointless lines because all components have been removed.

@chrisapril
Copy link
Author

Perfect :)

dsaltares added a commit that referenced this issue Oct 8, 2014
Fixes crash when trying to remove an invalid component from a PooledEntity #77
@dsaltares dsaltares added the bug label Oct 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants