libbdx box2d在createBody

时间:2016-09-04 17:43:46

标签: libgdx box2d jvm-crash

我的游戏因此消息崩溃:

# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000066bcbd0d, pid=13948, tid=12700
# [...]
# 
# Problematic frame:
# C  [gdx-box2d64.dll+0xbd0d]

从日志文件:

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.badlogic.gdx.physics.box2d.World.jniCreateBody(JIFFFFFFFFZZZZZF)J+0
j  com.badlogic.gdx.physics.box2d.World.createBody(Lcom/badlogic/gdx/physics/box2d/BodyDef;)Lcom/badlogic/gdx/physics/box2d/Body;+80
j  de.tennoxlab.cellolution.Food.<init>(Lde/tennoxlab/cellolution/CellWorld;FFZZ)V+115
j  de.tennoxlab.cellolution.CellWorld.generateFood(IFFFFZ)V+54
J 89 C2 de.tennoxlab.cellolution.CellWorld.update()V (159 bytes) @ 0x00000000024623b4 [0x0000000002461c60+0x754]
j  de.tennoxlab.cellolution.Cellolution.render()V+40
j  com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop()V+698
j  com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run()V+27
v  ~StubRoutines::call_stub

完整日志:http://pastebin.com/QjY3msYS

从所有其他类似崩溃的帖子中,可能会发生一些世界的修改。 在创建新的Food对象期间发生崩溃:

// generateFood() - called during World.update
for (int i = 0; i < count; i++) {
    float x = minX + (float) Math.random() * (maxX - minX);
    float y = minY + (float) Math.random() * (maxY - minY);
    Gdx.app.debug("World", "Generating food piece "+i);
    this.foods.add(new Food(this, x, y, animateGrowth, this.renderable));
}
在Food构造函数中

// in Food(...) constructor
BodyDef bodyDef = new BodyDef(); //TODO: set to sleep on init?
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(x, y);
bodyDef.linearDamping = 0.5f;
bodyDef.angularDamping = 1f;
Gdx.app.debug("Food", "Init body with " + bodyDef + " at " + x + "," + y);
foodBody = world.box2dWorld.createBody(bodyDef);  // <== HERE IS WHERE THE CRASH HAPPENS
CircleShape shape = new CircleShape();
shape.setRadius(getSizeFromEnergy());
fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.2f;
fixture = foodBody.createFixture(fixtureDef);
fixture.setUserData(this);
shape.dispose();

但它只是偶尔发生,有时是几分钟的游戏玩法(当generateFood代码成功执行了数百次)。所以我想,崩溃必定与食物生成之前世界发生的事情有关。所以我开始启用和禁用代码,直到我发现,崩溃只发生,当我的Cell对象的box2d体被破坏后

public void update() { // Cell.update - called during World.update
    if (this.energy <= 0) {
        world.cells.remove(this);
        Vector2 pos = cellBody.getPosition();
        Gdx.app.debug("Cell", "Destroying " + cellBody+" at "+pos.x+","+pos.y);
        world.box2dWorld.destroyBody(cellBody); // -> Without this line, the crashes don't happen!
        return;
    }
}

这是日志:

World: Generating food piece 99
Food: Init body with com.badlogic.gdx.physics.box2d.BodyDef@72aff016 at -28.201607,-56.101532
World: Removing cell: de.tennoxlab.cellolution.Cell@7cbb6f2c
Cell: Destroying com.badlogic.gdx.physics.box2d.Body@757ff1ad at -51.92446,-56.464954
World: Removing cell: de.tennoxlab.cellolution.Cell@7cbb6f2c
Cell: Destroying com.badlogic.gdx.physics.box2d.Body@757ff1ad at -51.92446,-56.464954
World: Generating 100 new food pieces
World: Generating food piece 0
Food: Init body with com.badlogic.gdx.physics.box2d.BodyDef@3828ef8f at 14.653984,96.249084
[...]
World: Generating food piece 21
Food: Init body with com.badlogic.gdx.physics.box2d.BodyDef@2b91d887 at -15.35305,13.934067
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000066bcbd0d, pid=9792, tid=13288
#

2 个答案:

答案 0 :(得分:0)

我自己解决了。这是一个讨厌的小虫子,可能像其他所有其他问题一样。

正如您在日志文件中看到的那样,同一个单元格被删除两次

World: Removing cell: de.tennoxlab.cellolution.Cell@7cbb6f2c
Cell: Destroying com.badlogic.gdx.physics.box2d.Body@757ff1ad at -51.92446,-56.464954
World: Removing cell: de.tennoxlab.cellolution.Cell@7cbb6f2c
Cell: Destroying com.badlogic.gdx.physics.box2d.Body@757ff1ad at -51.92446,-56.464954

当我意识到我很快发现了这个问题时:我已经将Player单元格添加到列表中两次,因此将其更新两次,将其破坏两次 - box2d显然不喜欢。

正如这些崩溃这样的“解决方案”如果你自己遇到这种崩溃并没有真正的帮助(因为你的错误将是其他东西),我只能建议通过严格的调试来找出问题。 (我在各处添加了大量的调试日志行并检查了输出 - 持续了好几个小时......)因为这些错误会发生。对于一些更频繁的,对于一些不太频繁,但它们发生。

我要添加的其他内容:box2d崩溃消息实在是无用的。真的很难找到,真正*导致崩溃的原因。太糟糕了。我希望有更多的调试信息,更多的安全检查和库本身(或libgdx,也许)的东西。

在游戏的持续发展过程中,我真的很期待更多那些(似乎)无法解决的错误/崩溃:Z

答案 1 :(得分:0)

你已经解决了你的问题但是如果有人在这个帖子上发现了问题:在我的情况下,问题是,Box2D世界被处理了两次。