Hi,
Is it a bad idea to create bodies/fixtures randomly in the tick method? I've been doing it and it works but then i sometimes get crashes (exc_bad_access).
I have the following in my tick method:
which is called based on if a random number between 1 and 60 equals 1. The crashes happen randomly, I don't seem to be doing anything specific when they happen, but also they occasionally happen when 2 bodies have collided (though not the any of the bodies created above) and are destroyed using DestroyBody.
The code crashes here:
which is in b2BlockAllocator.cpp
void* b2BlockAllocator::Allocate(int32 size)
and if I click down the thread on the left of xcode it goes to:
then
then
then my code
Can anyone help me understand what's going on?
Thanks
Is it a bad idea to create bodies/fixtures randomly in the tick method? I've been doing it and it works but then i sometimes get crashes (exc_bad_access).
I have the following in my tick method:
Code:
CCSprite *sprite = [CCSprite spriteWithFile:@"Icon.png" rect:CGRectMake(0, 0, 10, 10)];
[self addChild:sprite];
sprite.tag = 999;
b2BodyDef sBodyDef;
sBodyDef.type = b2_dynamicBody;
sBodyDef.position.Set((xPos+PosX)/PTM_RATIO, (yPos+PosY)/PTM_RATIO);
sBodyDef.userData = sprite;
sBodyDef.angle = CC_DEGREES_TO_RADIANS(randomAngle);
b2Body* sBody = gameWorldB2D->CreateBody(&sBodyDef);
b2PolygonShape poly;
poly.SetAsBox((rWidth)/PTM_RATIO, (rHeight)/PTM_RATIO);
b2FixtureDef sShapeDef;
sShapeDef.shape = &poly;
sShapeDef.density = rocketDensity;
sShapeDef.filter.maskBits = 0x0002;
b2Fixture * sFixture;
sFixture = sBody->CreateFixture(&sShapeDef);
[self addChild:sprite];
sprite.tag = 999;
b2BodyDef sBodyDef;
sBodyDef.type = b2_dynamicBody;
sBodyDef.position.Set((xPos+PosX)/PTM_RATIO, (yPos+PosY)/PTM_RATIO);
sBodyDef.userData = sprite;
sBodyDef.angle = CC_DEGREES_TO_RADIANS(randomAngle);
b2Body* sBody = gameWorldB2D->CreateBody(&sBodyDef);
b2PolygonShape poly;
poly.SetAsBox((rWidth)/PTM_RATIO, (rHeight)/PTM_RATIO);
b2FixtureDef sShapeDef;
sShapeDef.shape = &poly;
sShapeDef.density = rocketDensity;
sShapeDef.filter.maskBits = 0x0002;
b2Fixture * sFixture;
sFixture = sBody->CreateFixture(&sShapeDef);
which is called based on if a random number between 1 and 60 equals 1. The crashes happen randomly, I don't seem to be doing anything specific when they happen, but also they occasionally happen when 2 bodies have collided (though not the any of the bodies created above) and are destroyed using DestroyBody.
The code crashes here:
Code:
m_freeLists[index] = block->next;
which is in b2BlockAllocator.cpp
void* b2BlockAllocator::Allocate(int32 size)
and if I click down the thread on the left of xcode it goes to:
Code:
void* mem = allocator->Allocate(sizeof(b2PolygonShape));
then
Code:
m_shape = def->shape->Clone(allocator);
then
Code:
fixture->Create(allocator, this, def);
then my code
Code:
sFixture = sBody->CreateFixture(&sShapeDef);
Can anyone help me understand what's going on?
Thanks