Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!

puzzling bullet physics btVector3 issue

  • You cannot reply to this topic
2 replies to this topic

#1 dendrite   Members   -  Reputation: 105


Posted 27 May 2015 - 10:21 AM

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?


#2 JoeJ   Members   -  Reputation: 320


Posted 27 May 2015 - 01:52 PM

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);

#3 dendrite   Members   -  Reputation: 105


Posted Yesterday, 02:41 AM

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>'