First Chance Exception When Instantiating btConvexHullShape

Post Reply
at94
Posts: 2
Joined: Fri Aug 14, 2015 11:56 am

First Chance Exception When Instantiating btConvexHullShape

Post by at94 »

When I create a new btConvexHullShape I get a first chance exception. Code is:

Code: Select all

btConvexHullShape* m_collisionShapes;
m_collisionShapes = static_cast<btConvexHullShape*>(malloc(sizeof(btConvexHullShape)* MAX_BODY_COUNT));
new (&m_collisionShapes[m_activeBodyCount]) btConvexHullShape();
I also tried:

Code: Select all

std::vector<btConvexHullShape> m_hulls;
m_hulls.resize(MAX_BODY_COUNT);
The exception occurs at the new call and the resize call. The exception is:

Code: Select all

  Unhandled exception at 0x0102C983 in Useful_Engine.exe: 
0xC0000005: Access violation reading location 0xFFFFFFFF.
And it occurs inside of the bullet source code at:

Code: Select all

/**@brief Return the elementwise product of two vectors */
SIMD_FORCE_INLINE btVector3 
operator*(const btVector3& v1, const btVector3& v2) 
{
#if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
    return btVector3(_mm_mul_ps(v1.mVec128, v2.mVec128));
#elif defined(BT_USE_NEON)
    return btVector3(vmulq_f32(v1.mVec128, v2.mVec128));
#else
    return btVector3(
            v1.m_floats[0] * v2.m_floats[0], 
            v1.m_floats[1] * v2.m_floats[1], 
            v1.m_floats[2] * v2.m_floats[2]);
#endif
}
Is this a known problem ?
anthrax11
Posts: 72
Joined: Wed Feb 24, 2010 9:49 pm

Re: First Chance Exception When Instantiating btConvexHullSh

Post by anthrax11 »

Try something like this instead:

Code: Select all

btConvexHullShape** m_hulls = new btConvexHullShape*[MAX_BODY_COUNT];
for (int i=0; i<MAX_BODY_COUNT; i++) {
	m_hulls[i] = new btConvexHullShape();
}
Most bullet classes are required to have 16-byte alignment in memory, otherwise the fast SSE instructions will crash with an access violation exception. Bullet normally handles alignment for you by overloading the "new" operator (see BT_DECLARE_ALIGNED_ALLOCATOR), but that doesn't help if the memory is manually allocated.

If you do want to manually allocate memory for these classes, you need to use btAlignedAlloc(..., 16)/btAlignedFree instead of malloc/free or use btAlignedObjectArray<btConvexHullShape> as the container to ensure alignment and then initialize the objects with placement new. That's more complicated than it needs to be though, so I wouldn't recommend it.
Post Reply