Abort trap error with LibGDX and Box2D

I’m trying to build a game with LibGDX and Box2D.
In the game, I have stars to be collected and platforms to jump on. The starts are removed from the Box2D world when the player ‘gets’ them. They also removed from the world if they are not visible in the screen anymore. Platforms are removed from the world.

This is the first time I am getting an error like this (Well tons of times for this project, but never on anything else). It is totally random. I can’t pinpoint any lines or methods. I tried this on Eclipse, exported Jar file and on Android. Sometimes you can play the game for like forever, but sometimes it crashes in 1 minute.

There are no particular action to trigger it. My game is only about jumping and getting the coins, and these two are the only things you can do. On Eclipse, sometimes it just freezes, it doesn’t crash but it also does not do anything. But most of the times it gives this error.

Here is the error I am getting:

[quote]#

A fatal error has been detected by the Java Runtime Environment:

SIGSEGV (0xb) at pc=0x0000000110886978, pid=3221, tid=62211

JRE version: Java™ SE Runtime Environment (7.0_51-b13) (build 1.7.0_51-b13)

Java VM: Java HotSpot™ 64-Bit Server VM (24.51-b03 mixed mode bsd-amd64 compressed oops)

Problematic frame:

C [libgdx.dylib+0xa978] _ZN16b2BlockAllocator8AllocateEi+0x58

Failed to write core dump. Core dumps have been disabled. To enable core dumping, try “ulimit -c unlimited” before starting Java again

An error report file with more information is saved as:

/Users/mesut/hs_err_pid3221.log

If you would like to submit a bug report, please visit:

http://bugreport.sun.com/bugreport/crash.jsp

The crash happened outside the Java Virtual Machine in native code.

See problematic frame for where to report the bug.

Abort trap: 6
[/quote]
This the error report file at /Users/mesut: http://pastebin.java-gaming.org/0c1e98e648f

I know I’m not giving much detail, but if there is something I can do, please tell me. I have a messy project, but I can upload it to somewhere if that makes a difference.

Can you tell me what this means? Or the ways to overcome it? Thanks in advance :slight_smile:

I don’t think anyone here can tell you the exactly problem without knowing what your code is doing.
However the crash log does have some helpful information, the last point in your code before the crash is somewhere in your:

 com.mesut.jumper.entities.Entity.init(...) 

method at a point where you call:

 com.badlogic.gdx.physics.box2d.World.createBody() 

If you’re project seems messy to you, maybe the first place to start is cleaning it up and cleanly separating methods, classes, and packages so that you can better debug the code.

The crash could be caused by any number of reasons, such as allocating too many or too large buffers (i.e. ByteBuffer, FloatBuffer, etc.) or when creating too many sockets/database connections or incorrectly calling an OpenGL method.
Like the log says, the exception occurred in native code so your best bet is to start debugging your code starting in your Entity.init() method and testing your game a lot to figure out why the crash is occurring.

To get started, I would recommend:
Watch your program’s memory usage, CPU usage, number of open sockets, etc. Which can all be done on windows using Task Manager or Resource Monitor.
Add log messages to your code starting in Entity.init() and going backward from there so you can figure out which methods are passing incorrect parameters, initializing an object incorrectly, etc.

Debugging is a strange art, I would start at the point that the log message mentioned (inside Entity.init() when you call World.createBody()) and work up from there. Remember a method call before that method call could be the issue.

Thanks TeamworkGuy2.

My project doesn’t seem messy to me maybe just because I am the one who wrote it all, but I think it is messy, because I’m experimenting on the code.

I appreciate you took a look at the crash in the pastebin. I was planning to improve the game a little bit more, change the graphics from the ones I found on the internet which I don’t know the source to the ones that I could give credits for. But now I’m going to tidy up the code a little, and then put it to WIP section of the forum and try to work this error out.

Of course I’ll take your advise, and start from there. The main reason I asked for help is not the error itself, but the randomness of the error. I think I might have to rebuild that class. And the subclasses, because I have lots of classes which extend that one.

Native code is Box2D as you (and all others who read it) can guess if you know about it. I used to be able to fix those ones (Last one was an easy one, removing a body from the world, but keeping it for the update and render). But they generally gave me more obvious code locations.

I am keeping looking into it, but I won’t say no to any help offers in the mean time. Actually, I’d appreciate it. Any pointers, any hints, or anything I can help you to help me… I need anything I can get from you people.

When you remove bodies from box2d alway be sure that you only remove them once. Usual bug pattern is to add bodies to list at contact listener and then remove those without checking is same body multiple time in that list.

FWIW the crash occurs in native code inside Box2D attempting, by the look of it, to allocate some memory. You’d need the source code of libgdx to hand to see what’s going on in BlockAllocator8Allocate and why/how it can cause a trap.

Cas :slight_smile:

@pitbuller,
I know it’s the common mistake. The other is to try to remove a body while the world is still locked. I experienced them both, and I solved them both. This is not one of them. But thanks for help :slight_smile:

@princec,
I think libgdx has some methods to manually allocate memory. I’m now allowing it to handle this for me, because I am not sure about the amount to be allocated. But I hope I can solve this without having to take a look at the source code of LibGDX.

I kind of remember having this issue with Box2D code as well. Kind of amusing to see the cryptic error messages that non-Java developers have to work with :wink:

Unfortunately I do not remember what the precise cause is, but it could be something like pitbuller suggested. Did you try isolating the cause by backtracing functionality you recently added / changed? I.e. deactivating things you added or changed in an attempt to find the cause.

Here is a possible solution.

Anything that involves adding fixtures, removing fixtures from a body or creating a body during actual simulation requires you to do it OUTSIDE the physics step.

First thing, update logic > take input > render

In that order.

So at the start of your game loop this should be called before anything:



world.step(1f / 60f, 5, 8); // or any other values you desire


This will at least ensure that the world is doing a step before you start smashing keys/buttons.

Secondly, make this a habit before editing a world:



		if(!world.isLocked()){
			// DO stuff to box2d stuff, such as...
			world.destroyBody(body);
		}


Secondly, in any code you have that works with bodies such this:



/* Draw the Box2D sprites associated with the body */
		for (Entity entity : factory.getEntities()) {
			Sprite sprite = entity.getSprite() != null ? entity.getSprite()
					: null;
			Body body = entity.getBody();
			if (sprite != null && body != null) {
				sprite.setOrigin(0, 0);
				sprite.setPosition(body.getPosition().x, body.getPosition().y);
				sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
				sprite.draw(batch);
			}

		}


Check if your body is null before doing anything, just because you destroy a body does not mean the object holding the body is null.

So if I have an enemy and I kill him, I need to delete the box2d body safely but say you mess up your code order and you delete the body, then draw, then remove that entity from an array.

You just tried to draw an entity with a null body. Having a null check saves you but that does not mean your code is ideal, do it right and things like that technically should not happen.

@Grunnt,
I already do what pitbuller suggested as possible solutions, but I double checked what he said, thinking maybe I think I did them didn’t actually do them. I double checked them :slight_smile: Problem still exists.
The most annoying thing is that, it seems it’s completely random.

@Gibbo3771
Thanks. Right now, I’m doing as you said with logic>input>render. I also check whether the world is locked or not.

But I’m not checking if my body is null or not. In my initializing methods, I create the body first, then add a sprite to it. So I was thinking it’s not really necessary, because I create the body, put the sprite, and that method only called once. But I’ll try your suggestion for a more secure code (secure for me).

I have a hunch that this is about removing bodies but it worked well before…

While I was writing this answer, I got distracted and read the error code again. Check this out:

[quote]j com.mesut.jumper.utilities.Randomizer.test(Lcom/mesut/jumper/entities/Platform;)V+41
[/quote]
This is the method I am putting a star (to be collected) just one meter above the platform.
Here is my platform generating method and that test method:

public void generatePlatforms() {
float minGap = 3, maxGap = 5.5f;
float realGap = MathUtils.round(MathUtils.random(minGap, maxGap));
edge = getEdge();
float realWidth;
if (edge - gameScreen.getCamera().viewportHeight / 2 < gameScreen.getPlayer().body.getPosition().y) {
float platformY = edge;
realWidth = 2f * gameScreen.getReferencePlatform().texture.getWidth() * WTB;
bodyCenter = MathUtils.round(MathUtils.random(gameScreen.getScreenEdges().x + realWidth, gameScreen.getScreenEdges().y - realWidth));
Platform platform = new Platform(gameScreen.getSpriteBatch(), gameScreen.getWorld(), WTB, bodyCenter - realWidth / 2, bodyCenter + realWidth / 2, platformY + realGap);
gameScreen.getPlatforms().add(platform);
test(platform);
}
}

public void test(Platform p) {
Vector2 collectiblePoint = new Vector2(p.getX1(), p.getY() + 1f);
Coin c = new Coin(gameScreen.getSpriteBatch(), gameScreen.getWorld(), WTB, collectiblePoint);
gameScreen.getCollectibles().add(c);
//Coin class extends a class called Collectible. Collectibles is an array which holds collectibles to render. 
}

Now, let’s take a look at the first lines of the error:

[quote]C [libgdx.dylib+0xa978] b2BlockAllocator::Allocate(int)+0x58
C [libgdx.dylib+0x1fd96] b2World::CreateBody(b2BodyDef const*)+0x26
C [libgdx.dylib+0x27d4e] Java_com_badlogic_gdx_physics_box2d_World_jniCreateBody+0x9e
[/quote]
It seems to me that this is not about destroying the bodies or objects, but it’s about creating them. Right? If so, there must be something about the test(Platform p) method (I can’t remember why it’s called test).
Is it not a good idea to create a body using other body’s information? Maybe that is where I should put the if statement Gibbo talks about? Well I’m gonna try that and see if that’s the reason.

I’m still open to any kinds of advice :slight_smile: It’s nice to see other people’s ideas about this.

Your best course of action is to break your game down into chunks.

So disable things one by one until this problem no longer occurs, this will help you break it down.

Every time I get some sort of shit error like that it is always something to do with how you are creating/deleting stuff.

Are the stars floating right above the platforms? Or are they sitting on it? There could be a chance that they are created and overlapping, causing it to crash. Although this is not normally the case, it can happen.

However, if you are storing the last position of the platform made and using it to compare to to a new position, you should not have this issue.

I was suspecting it has something to do with how I destroy bodies from the world, but the error log says something about creating one (I guess).

Stars are floating, here:
https://www.dropbox.com/s/e40juifo7cgriy7/Screenshot%202014-03-18%2023.46.35.png (Seems like JGO doesn’t like Dropbox).

This is my process for platform and star creating: A platform is randomly created with a fixed width and height, the x and y location are randomly defined. Then the platform is put into the world. Then, as you can see in my test(Platform p) method, I take the x coordinate of the platform and make it star’s x, and take the y coordinate of the platform, and add 1 to it so that they don’t overlap, then set this as y of the star.
So, if a platform is located at 10, 12, then the star is at 10, 13.

Meanwhile, stars and platforms are kinematic bodies, they do not move and their location isn’t changed neither in my code by the user nor by any collision.

Now, I think I fixed it. But I am not sure how.
I checked my Entity class and its subclasses. The hierarchy and the relations between them were so messed up that I couldn’t tell where a variable was actually got a value. Fixed those. Got the error.
Then, I tried to simplify my Entity.init() method. Some of the code in that method belonged to the constructor. Did some trial and error, fixed those. And I haven’t got that error since. That makes 1 hour of playing that boring game. It should be okay.

At least that’s what I was thinking. It gave another error the last time I closed the game, the error was just after the shut down. And it is a different one this time. It happened once and it doesn’t say anything about my code, but it happened outside of JVM, in native code again.

Here: http://www.java-gaming.org/?action=pastebin&id=869

I’m not sure if my code can cause that. By the way, if I get my old error, I’ll head here again :slight_smile: