I have been having a recurrent "random" fatal error in the libgdx mini-game I am developping. For what I gathered, it is a box2d native error, related to a body creation. A possible cause of this is the generation of a body during a timestep, but I believe I have taken the appropriate counter measures.

The following case seems similar, but actually was about the removal of objects from an array while looping through it : [Deleting and creating body in libGDX](https://stackoverflow.com/questions/26082439/deleting-and-creating-body-in-libgdx) .

Regarding my own issue, here is the output of the crash :

< console >

```
EntityManager: entity generation begin ---------
IceSpike: generating physics
IceSpike: generated physics
IceSpike: generating graphics
EntityManager: entity generation registered
IceSpike: generating physics
IceSpike: generated physics
IceSpike: generating graphics
EntityManager: entity generation registered
IceSpike: generating physics
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000066bcbd0d, pid=5288, tid=0x00000000000013d4
#
# JRE version: Java(TM) SE Runtime Environment (8.0_111-b14) (build 1.8.0_111-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.111-b14 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [gdx-box2d64.dll+0xbd0d]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\Codage\projects\eclipse\git\spacegame\SpaceGame\android\assets\hs_err_pid5288.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
AL lib: (EE) alc_cleanup: 1 device not closed

```

Log file : <http://pastebin.com/JjBGnTri>

When it comes to the code, I will try to do as simple as possible. Hopefully, I am not cutting part of the issue.

I have an EntityManager which handles the generation and destruction of my entities. All entities in the game inherit from Entity (a big abstract blob). They all have a box2d body, which is generated in `generatePhysics(World physics)`.

**EntityManager**

```
private GameWorld world;
private Array<Entity> generation;
private Array<Entity> generationCpy;
private Array<Entity> trash;
private Array<Entity> trashCpy;

public void update(float delta)
{
    //generate new entities
    Gdx.app.debug(this.getClass().getSimpleName(), "entity generation begins ---------");
    if(generation.size != 0)
    {
        generationCpy = new Array<Entity>(generation);
        for (Entity entity : generationCpy)
        {
            entity.generate(world.getPhysics(),world.getAssetManager());
        }
    }

    //destroy old entities
    Gdx.app.debug(this.getClass().getSimpleName(), "entity destruction begins ---------");
    if(trash.size != 0)
    {
        trashCpy = new Array<Entity>(trash);
        for(Entity entity : trashCpy)
        {
            trash.removeValue(entity, true);
            entity.destroy(world.getPhysics());
            entity = null;
        }
    }
    Gdx.app.debug(this.getClass().getSimpleName(), "entity generation complete ------");

```

**Entity**

```
public final void generate(World physics, AssetManager assetManager)
{
    if(state != EntityState.GENERATED)
    {
        generatePhysics(physics);
        generateGraphics(assetManager);
        setState(EntityState.GENERATED);
    }
    else
        Gdx.app.error(this.getClass().getSimpleName(), "entity couldn't be generated. It had allready been generated");
}

```

So far, only the spikes seem to be concerned, and most of the time, everything runs smoothly. Rest of the time, this is the crashing part:

**IceSpikes**

```
@Override
public void generatePhysics(World physics)
{
    Gdx.app.debug(this.getClass().getSimpleName(), "generating physics");

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(this.position.cpy().scl(1 / PPM));

    body = physics.createBody(bodyDef);
    body.setBullet(true);

    PolygonShape shape = new PolygonShape();
    Vector2 vertices[] = new Vector2[3];
    vertices[0] = new Vector2(-width/2,-height/2).scl(1f/PPM);
    vertices[1] = new Vector2(0,height/2).scl(1f/PPM);
    vertices[2] = new Vector2(width/2,-height/2).scl(1f/PPM);
    shape.set(vertices);        

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1f;
    fixtureDef.restitution = 1f;
    fixtureDef.filter.categoryBits = CollisionManager.BIT_PROJECTILE;
    fixtureDef.filter.maskBits = CollisionManager.BIT_PLAYER;

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(this);

    shape.dispose();

    Gdx.app.debug(this.getClass().getSimpleName(), "generated physics");
}

```

This suggests it is due to the timing creation of IceSpikes, which is after a collision (in a world.step()). However, this is precisely the point of my whole entity management system. It generates/removes bodies outside of a world.step(), as seems confirmed by the output.

Also, copied arrays are set up to prevent entities to be removed from the generation and trash array while theyre are being looped through.

I must have overlooked something, but what ? Any idea how to proceed from there ?