🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

puzzling bullet physics btVector3 issue

Started by
1 comment, last by dendrite 9 years, 1 month ago

While testing out bullet physics, I created a simple debug build of a test program. However, about half the time, it crashes with the following error:
Unhandled exception at 0x0098D907 in game.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF.

This is the source:

btVector3 inertia(0, 0, 0);
btScalar mass = 0.0;
std::shared_ptr<btDefaultMotionState> motionState = std::make_shared<btDefaultMotionState>(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
btCollisionShape* shape = new btBoxShape(btVector3(2, 2, 2));
shape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo info(mass, motionState.get(), shape, inertia);
std::shared_ptr<btRigidBody> body = std::make_shared<btRigidBody>(info); // it crashes when execution reaches here

Visual studio 2013 says this line in btVector3.h is generating the exception:
mVec128 = (__m128)_mm_xor_ps(mVec128, mVec128); // line 675

Any ideas what may be going on?

Advertisement

I've never used shared pointers and forgot anything about Bullet, but i bet it's an alignment issue:

btCollisionShape* shape = new btBoxShape(btVector3(2, 2, 2));

You allocate a BoxShape with new, if the BoxShape contains SIMD data (like a btVector3) it may crash sometimes because new does not align the memory.

If i'm right, then Bullet should provide a "create box shape" function that returns the aligned pointer to the correctly allocated shape data.

If this is new to you, be warned that this can cause hard to find bugs and causes some frustration, for example:

std::vector<btVector3> stuff;

This will simply not work with Microsoft STL (or has something changed meanwhile?)

Bullet may use aligned containers internally, you can use them as well or you can make your own...

Here is a example line to allocate 16 bytes aligned memory: sampleNodes = (SampleNode*)_aligned_malloc (sizeof(SampleNode) * sampleNodesSize, 16);

Yeah, I'm not familiar with how to align things in c++. I'm trying the following:

std::shared_ptr<btCollisionShape> shape = std::allocate_shared<btBoxShape>(boost::alignment::aligned_allocator<btBoxShape, 16>, btVector3(2, 2, 2));

This produces the following error:

error C2275: 'boost::alignment::aligned_allocator<btBoxShape,16>' : illegal use of this type as an expression

I get the same error when using the included btAlignedAllocator as well. I've included all the appropriate include files, so I can't figure out what the issue is. I tried declaring the allocator like this:

boost::alignment::aligned_allocator<btBoxShape, 16> shapeAlloc;

This compiles fine. Putting in shapeAlloc as a parameter for the allocator in make_shared produces this error instead:

error C2079: 'std::_Ref_count_obj_alloc<_Ty,_Alloc>::_Myal' uses undefined class 'boost::alignment::aligned_allocator<U,16>'

This topic is closed to new replies.

Advertisement