Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (593)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (647)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Abort trap error with LibGDX and Box2D  (Read 1133 times)
0 Members and 1 Guest are viewing this topic.
Offline kutucuk

Senior Devvie

Medals: 5
Exp: 3 years

« Posted 2014-03-17 14:17:40 »

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:
# A fatal error has been detected by the Java Runtime Environment:
#  SIGSEGV (0xb) at pc=0x0000000110886978, pid=3221, tid=62211
# JRE version: Java(TM) SE Runtime Environment (7.0_51-b13) (build 1.7.0_51-b13)
# Java VM: Java HotSpot(TM) 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:
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
Abort trap: 6

This the error report file at /Users/mesut:

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 Smiley
Offline TeamworkGuy2

Junior Devvie

Medals: 10

« Reply #1 - Posted 2014-03-17 16:38:33 »

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:

method at a point where you call:

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.
Offline kutucuk

Senior Devvie

Medals: 5
Exp: 3 years

« Reply #2 - Posted 2014-03-17 17:58:11 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2014-03-17 18:33:16 »

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.
Offline princec

« JGO Spiffy Duke »

Medals: 545
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

« Reply #4 - Posted 2014-03-17 19:09:22 »

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 Smiley

Offline kutucuk

Senior Devvie

Medals: 5
Exp: 3 years

« Reply #5 - Posted 2014-03-17 19:18:16 »

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 Smiley

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.
Offline Grunnt

JGO Kernel

Medals: 95
Projects: 8
Exp: 5 years

Complex != complicated

« Reply #6 - Posted 2014-03-18 13:56:42 »

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.

Offline Gibbo3771
« Reply #7 - Posted 2014-03-18 17:14:24 »

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:

         // DO stuff to box2d stuff, such as...

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);


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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline kutucuk

Senior Devvie

Medals: 5
Exp: 3 years

« Reply #8 - Posted 2014-03-18 18:11:11 »

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 Smiley Problem still exists.
The most annoying thing is that, it seems it's completely random.

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:
j  com.mesut.jumper.utilities.Randomizer.test(Lcom/mesut/jumper/entities/Platform;)V+41
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);

public void test(Platform p) {
Vector2 collectiblePoint = new Vector2(p.getX1(), p.getY() + 1f);
Coin c = new Coin(gameScreen.getSpriteBatch(), gameScreen.getWorld(), WTB, collectiblePoint);
//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:
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

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 Smiley It's nice to see other people's ideas about this.
Offline Gibbo3771
« Reply #9 - Posted 2014-03-18 19:59:44 »

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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kutucuk

Senior Devvie

Medals: 5
Exp: 3 years

« Reply #10 - Posted 2014-03-18 21:49:54 »

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: (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.
Offline kutucuk

Senior Devvie

Medals: 5
Exp: 3 years

« Reply #11 - Posted 2014-03-19 19:33:05 »

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.


I'm not sure if my code can cause that. By the way, if I get my old error, I'll head here again Smiley
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

deepthought (38 views)
2015-06-30 15:39:44

deepthought (42 views)
2015-06-30 15:39:09

deepthought (53 views)
2015-06-30 15:36:52

Za\'Anzabar (24 views)
2015-06-29 05:44:54

TritonDreyja (40 views)
2015-06-24 17:10:40

CopyableCougar4 (36 views)
2015-06-23 00:34:45

BurntPizza (42 views)
2015-06-21 20:36:46

cookiecompiler (85 views)
2015-06-11 15:42:53

cookiecompiler (48 views)
2015-06-11 15:41:14

NegativeZero (76 views)
2015-06-11 09:49:18
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!