I'm experiencing a peculiar crash in a very specific scenario (albeit a scenario that is not entirely uncommon in my case) after calling b2Body::SetTransform.
I've cut down the majority of my game's code and I've managed to reproduce the same bug I'm seeing with the following test case:
When bodyStaticA->SetTransform() is called for the second time around, I get a crash in b2ContactManager::AddPair on the following line:
fixtureA is at this point set to 0xfdfdfdfd, and my full callstack looks like this:
I'm using an entirely unmodified version of Box2D 2.2.1 with Visual Studio 2008 SP1. The specific problem outlined above only occurs in debug builds. The same thing occurs under Debian 6 using Box2D 2.2.1 and g++ (Debian 4.4.5-8) 4.4.5 - again, compiling with _DEBUG defined. I'm going out on a limb, here, but could this be a bug in b2BlockAllocator?
Any help would be greatly appreciated, and thanks for an excellent physics library.
I've cut down the majority of my game's code and I've managed to reproduce the same bug I'm seeing with the following test case:
Code:
#include <Box2D/Box2D.h>
b2World* world;
b2BodyDef bodyDefDynamic;
b2BodyDef bodyDefStatic;
b2Body* bodyDynamic;
b2Body* bodyStaticA;
b2Body* bodyStaticB;
b2FixtureDef fixtureDef;
void CreateBodies()
{
bodyDynamic = world->CreateBody(&bodyDefDynamic);
bodyDynamic->CreateFixture(&fixtureDef);
bodyDynamic->SetType(b2_staticBody);
bodyStaticA = world->CreateBody(&bodyDefStatic);
bodyStaticA->CreateFixture(&fixtureDef);
bodyStaticA->SetTransform(bodyStaticA->GetPosition(), bodyStaticA->GetAngle());
bodyStaticB = world->CreateBody(&bodyDefStatic);
bodyStaticB->CreateFixture(&fixtureDef);
bodyDynamic->SetType(b2_dynamicBody);
bodyDynamic->GetFixtureList()->SetFilterData(bodyDynamic->GetFixtureList()->GetFilterData());
}
void DestroyBodies()
{
world->DestroyBody(bodyDynamic);
world->DestroyBody(bodyStaticA);
world->DestroyBody(bodyStaticB);
}
int main()
{
world = new b2World(b2Vec2(0.0f, 0.0f));
b2CircleShape shapeCircle;
shapeCircle.m_radius = 1.0f;
fixtureDef.shape = &shapeCircle;
bodyDefDynamic.type = b2_dynamicBody;
bodyDefStatic.type = b2_staticBody;
CreateBodies();
DestroyBodies();
CreateBodies();
DestroyBodies();
delete world;
return 0;
}
b2World* world;
b2BodyDef bodyDefDynamic;
b2BodyDef bodyDefStatic;
b2Body* bodyDynamic;
b2Body* bodyStaticA;
b2Body* bodyStaticB;
b2FixtureDef fixtureDef;
void CreateBodies()
{
bodyDynamic = world->CreateBody(&bodyDefDynamic);
bodyDynamic->CreateFixture(&fixtureDef);
bodyDynamic->SetType(b2_staticBody);
bodyStaticA = world->CreateBody(&bodyDefStatic);
bodyStaticA->CreateFixture(&fixtureDef);
bodyStaticA->SetTransform(bodyStaticA->GetPosition(), bodyStaticA->GetAngle());
bodyStaticB = world->CreateBody(&bodyDefStatic);
bodyStaticB->CreateFixture(&fixtureDef);
bodyDynamic->SetType(b2_dynamicBody);
bodyDynamic->GetFixtureList()->SetFilterData(bodyDynamic->GetFixtureList()->GetFilterData());
}
void DestroyBodies()
{
world->DestroyBody(bodyDynamic);
world->DestroyBody(bodyStaticA);
world->DestroyBody(bodyStaticB);
}
int main()
{
world = new b2World(b2Vec2(0.0f, 0.0f));
b2CircleShape shapeCircle;
shapeCircle.m_radius = 1.0f;
fixtureDef.shape = &shapeCircle;
bodyDefDynamic.type = b2_dynamicBody;
bodyDefStatic.type = b2_staticBody;
CreateBodies();
DestroyBodies();
CreateBodies();
DestroyBodies();
delete world;
return 0;
}
When bodyStaticA->SetTransform() is called for the second time around, I get a crash in b2ContactManager::AddPair on the following line:
Code:
b2Body* bodyA = fixtureA->GetBody();
fixtureA is at this point set to 0xfdfdfdfd, and my full callstack looks like this:
Code:
> Box2D Test Case.exe!b2Fixture::GetBody() Line 275 + 0x3 bytes C++
Box2D Test Case.exe!b2ContactManager::AddPair(void * proxyUserDataA=0x00329738, void * proxyUserDataB=0x00329778) Line 188 + 0x8 bytes C++
Box2D Test Case.exe!b2BroadPhase::UpdatePairs<b2ContactManager>(b2ContactManager * callback=0x00329240) Line 218 C++
Box2D Test Case.exe!b2ContactManager::FindNewContacts() Line 175 C++
Box2D Test Case.exe!b2Body::SetTransform(const b2Vec2 & position={...}, float angle=0.00000000) Line 426 C++
Box2D Test Case.exe!CreateBodies() Line 24 C++
Box2D Test Case.exe!main() Line 52 C++
Box2D Test Case.exe!__tmainCRTStartup() Line 586 + 0x19 bytes C
Box2D Test Case.exe!mainCRTStartup() Line 403 C
Box2D Test Case.exe!b2ContactManager::AddPair(void * proxyUserDataA=0x00329738, void * proxyUserDataB=0x00329778) Line 188 + 0x8 bytes C++
Box2D Test Case.exe!b2BroadPhase::UpdatePairs<b2ContactManager>(b2ContactManager * callback=0x00329240) Line 218 C++
Box2D Test Case.exe!b2ContactManager::FindNewContacts() Line 175 C++
Box2D Test Case.exe!b2Body::SetTransform(const b2Vec2 & position={...}, float angle=0.00000000) Line 426 C++
Box2D Test Case.exe!CreateBodies() Line 24 C++
Box2D Test Case.exe!main() Line 52 C++
Box2D Test Case.exe!__tmainCRTStartup() Line 586 + 0x19 bytes C
Box2D Test Case.exe!mainCRTStartup() Line 403 C
I'm using an entirely unmodified version of Box2D 2.2.1 with Visual Studio 2008 SP1. The specific problem outlined above only occurs in debug builds. The same thing occurs under Debian 6 using Box2D 2.2.1 and g++ (Debian 4.4.5-8) 4.4.5 - again, compiling with _DEBUG defined. I'm going out on a limb, here, but could this be a bug in b2BlockAllocator?
Any help would be greatly appreciated, and thanks for an excellent physics library.