Game crashes when I try to use the features of a b2Body -- why?


My app crashes when I try to use the features of b2Body from within my NSObject class.

Here is the interface of my class:

#import <Foundation/Foundation.h>

#import "Box2D.h"
#import "GLES-Render.h"
#import "cocos2d.h"
#import "HelloWorldScene.h"

@interface Magnet : NSObject {
    b2Body* body;
@property (readwrite,assign) b2Body* body;

And here is how I declare it in HelloWorldScene.h:

#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#import <UIKit/UIKit.h>
#import "Magnet.h"

@class Magnet;

@interface HelloWorld : CCLayer {
    b2World* world;
    GLESDebugDraw *m_debugDraw;
    Magnet *magnet;

And here is how I access the b2Body in magnet, in

b2BodyDef bodyDef2;
bodyDef2.type = b2_staticBody;

bodyDef2.position.Set(300/PTM_RATIO, 150/PTM_RATIO);
bodyDef2.userData = sprite2;
magnet.body = world->CreateBody(&bodyDef2);

// Define another box shape for our dynamic body.
//b2PolygonShape dynamicBox;
b2CircleShape dynamicBox2;

//dynamicBox.SetAsBox(1.0f, 1.0f);//These are mid points for our 1m box
dynamicBox2.m_radius = 0.49;
// Define the dynamic body fixture.
b2FixtureDef fixtureDef2;

fixtureDef2.shape = &dynamicBox2;   
fixtureDef2.density = 1.3f;
fixtureDef2.friction = 1.0f;


Is there any problem with my code?

I'm receiving a SIGABRT in the Box2D source files, in the CreateFixture() function. What should I do?

Sorry about the mess, here is the backtrace:

    (gdb) bt
    #0  0x0002f35c in b2Body::CreateFixture (this=0x0, def=0xbfffd360) at b2Body.cpp:141
    #1  0x00004f7e in -[HelloWorld loadLevel:] (self=0x5544770, _cmd=0xfbc7c, level=1) at
    #2  0x0000573d in -[HelloWorld init] (self=0x5544770, _cmd=0x4ffd774) at
    #3  0x0009d7b1 in +[CCNode node] (self=0x147100, _cmd=0xfc558) at CCNode.m:231
    #4  0x0000377d in +[HelloWorld scene] (self=0x147100, _cmd=0xfb61c) at
    #5  0x000036d7 in -[MagnetSpillAppDelegate applicationDidFinishLaunching:] (self=0x680b9f0, _cmd=0xca40a3, application=0x5511370) at
    #6  0x008b1ce2 in -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] ()
    #7  0x008b3d88 in -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] ()
    #8  0x008be617 in -[UIApplication handleEvent:withNewEvent:] ()
    #9  0x008b6abf in -[UIApplication sendEvent:] ()
    #10 0x008bbf2e in _UIApplicationHandleEvent ()
    #11 0x029ca992 in PurpleEventCallback ()
    #13 0x013ddcf7 in __CFRunLoopDoSource1 ()
    #14 0x013daf83 in __CFRunLoopRun ()
    #15 0x013da840 in CFRunLoopRunSpecific ()
    #16 0x013da761 in CFRunLoopRunInMode ()
    #17 0x008b37d2 in -[UIApplication _run] ()
    #18 0x008bfc93 in UIApplicationMain ()
    #19 0x00002cc4 in main (argc=1, argv=0xbffff058) at main.m:13
asked on Stack Overflow May 15, 2011 by JulenissensHjelper • edited May 16, 2011 by JulenissensHjelper

1 Answer


Without the crash, it is hard to say specifically what is going wrong.

However, that code is rife with potential for crashes.

  • you are using structures that are on the stack, not allocated. If the scope of that code goes away before you are done with those structures, it'll lead to crashes and undefined behavior.

  • you aren't initializing all the fields in those structures; the remaining values will be in an undefined state. Since it is impossible to differentiate an uninitialized float32 from an initialized float32, that is bound to eventually lead to problems.

  • you are setting up structures on the stack that have references to other structures on the stack; another form of the first point, but should be called out because it makes it doubly easy to leak pointers to the stack.

Line 141 of b2Body::CreateFixture() is an assert:

    b2Assert(m_world->IsLocked() == false);

Is your world locked? Looks like it shouldn't be.

answered on Stack Overflow May 15, 2011 by bbum • edited May 16, 2011 by bbum

User contributions licensed under CC BY-SA 3.0