Search code examples
javacrashbox2d

Expression: IsLocked() == false, crashing when World is referenced?


The Error

AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!

Program: C:\Program Files\Java\jre1.8.0_25\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Dynamics/b2World.cpp, Line 109

Expression: IsLocked() == false

The following line is the last line before the error occurs:

Body body = world.createBody(bodyDef);

I don't understand how this is causing a problem. world and bodyDef aren't null.

How my program is structured is that I have a GameStateRegular class that in its init method, it adds two new VirusEntity objects, like so:

VirusEntity virus0 = new VirusEntity(world, mother, new Vector2(0, 0));
VirusEntity virus1 = new VirusEntity(world, mother, new Vector2(100, 100));
viruses.add(virus0);
viruses.add(virus1);

And this works fine. I have a VirusEntity set to do the following code to do when it begins a Contact with the MotherCellEntity:

private void setContactListener() {
    world.setContactListener(new ContactAdapter() {
        @Override
        public void beginContact(Contact contact) {
            Body bodyA = contact.getFixtureA().getBody();
            Body bodyB = contact.getFixtureB().getBody();

            if (bodyA == mother.getBody()) work(bodyA, bodyB);
            else if (bodyB == mother.getBody()) work(bodyB, bodyA);
        }

        private void work(Body motherCellBody, Body virusBody) {
            VirusEntity virus = null;
            for (VirusEntity v : viruses) {
                if (v.getBody() == virusBody) virus = v;
            }
            virus.remove();
            VirusEntity newVirus = new VirusEntity(world, mother, virus.getSpawnPosition());
            viruses.add(newVirus);
        }
    });
}

I am referencing the same World object, and the same MotherCellEntity object to the new VirusEntity that is being created, but I still cannot tell what is going on here.

Full GameStateRegular class: http://pastebin.com/mFpmp28T
Full VirusEntity class: http://pastebin.com/HjSGS4ER

Thank you all in advanced for any help. I am being very vague on this topic because I really don't understand what's happening. I tried to do some background research on this, but I was unsuccessful.


Solution

  • How I was Successful

    What I did was inside of my update method in my GameStateRegular class:

    @Override
    public void update() {
        world.step(1, 1, 1);
        mother.update();
        for (int i = 0; i < viruses.size(); i++) {
            VirusEntity e = viruses.get(i);
            if (e.isRemoved()) {
                viruses.remove(e);
                e.dispose();
                // --v--
                VirusEntity newVirus = new VirusEntity(world, mother, e.getSpawnPosition());
                viruses.add(newVirus);
                // --^--
            } else e.update();
        }
    }
    

    Also, for my ContactAdapter:

    private void setContactListener() {
        world.setContactListener(new ContactAdapter() {
    
            @Override
            public void preSolve(Contact contact, Manifold manifold) {
                Body bodyA = contact.getFixtureA().getBody();
                Body bodyB = contact.getFixtureB().getBody();
    
                if (bodyA == mother.getBody()) virusInvolved(bodyA, bodyB);
                else if (bodyB == mother.getBody()) virusInvolved(bodyB, bodyA);
            }
    
            private void virusInvolved(Body motherCellBody, Body virusBody) {
                VirusEntity virus = null;
                for (VirusEntity v : viruses) {
                    if (v.getBody() == virusBody) virus = v;
                }
                if (virus != null) {
                    virus.setMovementAllowed(false);
                    virus.remove();
                }
            }
        });
    }