'Box2D crash in b2BlockAllocator.cpp
I have just begun using Box2D with C++ and Visual Studio 2015 recently and am experiencing a very strange bug in which I get an access violation caused by b2BlockAllocator.cpp at line 155. It appears that an array is assigned to some sort of b2Block linked list structure, whose next value is null. This is what triggers the access violation.
if (m_freeLists[index])
{
b2Block* block = m_freeLists[index];
m_freeLists[index] = block->next; // <- error occurs here.
return block;
}
else
{...
But here's what's weird. I have been developing a game library for personal use, and the following code is used to instantiate a b2Body in a PhysicsObject class in the game library:
void PhysicsObject::onCreate()
{
/**/
b2BodyDef bodyDef;
bodyDef.type = m_bodyType;
bodyDef.position.Set((float)(m_x + m_width / 2) / (float)m_pLevel->getTileSize(), (float)(m_y + m_height / 2) / (float)m_pLevel->getTileSize());
m_pBody = m_pLevel->getWorld().CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox((float)m_width / (float)m_pLevel->getTileSize() * 0.5f, (float)m_height / (float)m_pLevel->getTileSize() * 0.5f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
m_pBody->CreateFixture(&fixtureDef); // <- This call is the last executed before the error.
/**/
}
This is in the game library, which is a Visual Studio DLL project, and it works perfectly fine. However, when I move this EXACT code to a class that derives from PhysicsObject in a test project that references the game library, the crash occurs. Allocating each object to the heap doesn't make a difference either, as the Box2D documentation states that the objects they are passed to do not keep the references. If this helps, Box2D is a statically linked library. Both the test project and the game library have the exact same dependencies and are in the same configuration. Does anyone have an idea as to why this might be happening? Any help greatly appreciated.
Update:
I have discovered what is causing the crash, but I'm not sure how exactly to go about fixing it. The issue is that Box2D's block allocator is becoming corrupt somehow after the b2Body is being created by the world. This is what the list of free chunks looks like before the allocation:
And after the b2Body is created:
As you can see, there is clearly corrupted memory in the chunk list. Does anyone know why this is happening? Would statically linking my project solve this?
Solution 1:[1]
The issue I was experiencing had to do with corrupted memory that occurred between the transfer from code in the DLL to the code in the executable. While I'm not exactly sure why this was, I think it might have had something to do with DLL Hell. The solution was to make my game library a static library instead of a dynamic one. This just compiled all the code directly into the executable and no corrupted memory caused by the transition could occur.
Solution 2:[2]
I set the userData incorrectly when creating the b2BodyDef. I think the userData is causing memory problems and something is wrong when the b2Body is destroyed. The problem was resolved after setting the userData correctly.
Solution 3:[3]
My game does not crash in BlockAllocator, but it does so in b2Contact::Destroy(). What happened to me was that:
- I called b2World::Step()
- Have a b2ContactListener-derived class listened for collisions for gameplay logic. b2ContactListener::BeginContact() is called
- b2ContactListener::BeginContact() called a function f() somewhere else.
- f() made a call that made b2World re-update.
- b2World deleted some b2Contacts, which were still on the stack at 2)
- When the logic is done, I was brought back to BeginContact() at 2). BeginContact() then accessed deleted objects, which caused crash.
I hope this is helpful for crash troubleshooting. My Visual Studio debugger also shows unreadable memory, so it confused me a deal lot. I thought it was Box2D fault but... hah. Bugs are made b
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mackinnon Buck |
| Solution 2 | Christmalo.win |
| Solution 3 | Revol Noom |
