EXC_BAD_ACCESS trying to access Box2D body UserData

582 views Asked by At

As suggested in the Box2D manual, I'm setting my body->userData to the CCSprite associated with it. However, when I later try to access it - even within the same method, or the same for loop - it inevitably throws EXC_BAD_ACCESS.

The error shows up any time the following code structure appears:

b2BodyDef bodyDef;
b2Body* blockBody;
Block* block;

//...

block = [Block spriteWithFile:spriteName];

//Block configuration
block.position = CGPointMake(x.floatValue * BLOCK_SIZE_PIXELS, y.floatValue * BLOCK_SIZE_PIXELS);        
block.anchorPoint = CGPointZero;
[self addChild:block z:7]; 
[self.blockArray addObject:block];

//Body configuration
bodyDef.type = b2_dynamicBody;
bodyDef.position = toMeters(block.position);
bodyDef.angle = 0.0f;
bodyDef.linearDamping = 1.0f;
bodyDef.angularDamping = 0.0f;
bodyDef.gravityScale = 1.0f;
bodyDef.allowSleep = true;
bodyDef.awake = true;
bodyDef.fixedRotation = true;
bodyDef.userData = █

blockBody = world->CreateBody(&bodyDef);

vertices[0] = toMeters(CGPointZero);  
vertices[1] = toMeters(CGPointMake(BLOCK_SIZE_PIXELS, 0));
vertices[2] = toMeters(CGPointMake(BLOCK_SIZE_PIXELS, BLOCK_SIZE_PIXELS));
vertices[3] = toMeters(CGPointMake(0, BLOCK_SIZE_PIXELS));

blockShape.Set(vertices, 4);

fixtureDef.shape = &blockShape;
fixtureDef.density = 1.0f;
blockFixture = blockBody->CreateFixture(&fixtureDef);

block.body = blockBody;


//UPON INVOKING THIS LINE,
//PROGRAM CRASHES WITH EXCEPTION: EXC_BAD_ACCESS
Block* test = (__bridge Block*)blockBody->GetUserData();

This code appears in loadlevel: in GameLayer.m, which inherits from CCLayer.

Block inherits from GameSprite, which defines .body and inherits from CCSprite. Header files:

//
//  Block.h
//
//  Created by [REDACTED] on 7/20/12.
//  Copyright (c) 2012 [REDACTED]. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GameSprite.h"
//#import "Constants.h"

@interface Block : GameSprite
{
//    GameSprite* _sprite;
//    int _platformTag;
}

+(Block*) blockWithFile: (NSString*) filename;
+(Block*) blockWithTexture: (CCTexture2D*) texture;
@end

//
//  GameSprite.h
//  LegendaryOiramBrothers
//
//  Created by [REDACTED] on 7/20/12.
//  Copyright (c) 2012 [REDACTED]. All rights reserved.
//

#import "CCSprite.h"
#import "Constants.h"
#import "Box2D.h"

@interface GameSprite : CCSprite
{
    b2Body* body;
}

- (void) update;

- (void) setPosition:(CGPoint)position;
- (b2Vec2) getVelocity;
- (void) setVelocity:(b2Vec2) vel;

@property (nonatomic) b2Body* body;

@end

From what I can find online, my problem seems to be an incorrect autorelease somewhere - but I don't see where the block would have an opportunity to deallocate. As a result, I am rather confused.

Thanks for your help.

1

There are 1 answers

1
CodeSmile On BEST ANSWER

Can you spot the error in this line?

bodyDef.userData = &block;

You're assigning the pointer's address, not the pointer itself. This should fix it:

bodyDef.userData = block;