I\'ve been trying to use XMVECTOR as a class member for a bounding box, since I do a lot of calculations, but I use the XMFLOAT3 only once per frame, so the bounding box has
The class is not being created at an aligned address, so even though the XM* members are aligned on 16-byte boundaries, the parent's alignment miss-aligns them, causing the crash.
To prevent this you need to use _mm_alloc
(which really just wraps _aligned_alloc), or replace the default allocator with one that returns blocks minimally aligned to 16 bytes (under x64 this the case with the default allocator).
a simple solution to this in C++ is to create a base class for all classes the contain XM* members that looks like the following:
template<size_t Alignment> class AlignedAllocationPolicy
{
public:
static void* operator new(size_t size)
{
return _aligned_malloc(size,Alienment);
}
static void operator delete(void* memory)
{
_aligned_free(memory);
}
};
class MyAlignedObject : public AlignedAllocationPolicy<16>
{
//...
};
As pointed out by @Dave, this is a minimal example, you'd want to overload all the new
and delete
operators, specifically new[]
and delete[]