i have a problem that after creating many box2d bodies and adding them to the world my app crashes with an EXEC_BAD_ACCESS error when iterating the bodies inside the world, here's how i create the bodies:
for(CXMLElement *node in obstaclesArr)
{
b2BodyDef nObstacleBody;
b2Body *obsBody;
CCSprite *obstacle;
obstacle = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@.png",[[node attributeForName:@"body"]stringValue]]];
NSString *strX = [[node attributeForName:@"x"]stringValue];
NSString *strY = [[node attributeForName:@"y"]stringValue];
float x;
float y;
x = [strX floatValue];
y = [strY floatValue];
obstacle.tag = E;
obstacle.position = ccp(x,y);
[self addChild:obstacle z:9];
nObstacleBody.type = b2_staticBody;
nObstacleBody.position.Set(x/PTM_RATIO, y/PTM_RATIO);
nObstacleBody.userData = obstacle;
obsBody = world->CreateBody(&nObstacleBody);
[[GB2ShapeCache sharedShapeCache]addFixturesToBody:obsBody forShapeName:[[node attributeForName:@"body"]stringValue]];
[obstacle setAnchorPoint:[[GB2ShapeCache sharedShapeCache]anchorPointForShape:[[node attributeForName:@"body"]stringValue]]];
}
And here's where the crash occurs:
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);//<-- crashes in this line
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
I have no clue as to why is crashing, can someone please help me?
You can avoid crash by checking Userdata type in b2Body.
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite *myActor = (CCSprite*)b->GetUserData();
if(myActor && [myActor isKindOfClass:[CCSprite class]] )
{
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);//<-- crashes in this line
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}