Question

I've been creating a flocking swarm project in DirectX 11 and had an error where I try and create a vector of Pointers to objects and on the 3rd iteration of pushing back it will crash and jump to this:

XMFINLINE _XMMATRIX& _XMMATRIX::operator=
(
    CONST _XMMATRIX& M
)
{
    r[0] = M.r[0];
    r[1] = M.r[1];
    r[2] = M.r[2];
    r[3] = M.r[3];
    return *this;
}

the crash happens when I execute this code:

Populate();

for (unsigned int i = 0; i < m_numOfPrey; i++)
{
    m_preyVec[i]->LoadContent(dx, colour, yaw, pitch, roll, scale);

    srand (time(NULL));
    XMFLOAT3 tempPos;

    tempPos.x = rand() % 40;
    tempPos.y = rand() % 40;
    tempPos.z = rand() % 40;

    m_preyVec[i]->SetPosition(tempPos);
}

return true;

Populate function:

for (unsigned int i = 0; i < m_numOfPrey; i++)
{
    Prey* newPrey = new Prey();
    m_preyVec.push_back(newPrey);
}

EDIT: the prey inherits from a vehicle class (which builds the buffers, FX and InputLayout for the vehicle used my the Prey and Predator.

so this is the Prey LoadContent();

{
    Vehicle::LoadContent(dx, colour, yaw, pitch, roll, scale);
    return true;
}

and this is the Vehicle LoadContent();

{
    if(!GameObject::LoadContent(dx, m_position, colour, yaw, pitch, roll, scale))
    {
        return false;
    }

    if(!BuildBuffers(dx))
    {
        return false;
    }

    if(!BuildFX(dx))
    {
        return false;
    }

    if(!BuildInputLayout(dx))
    {
        return false;
    }

    return true;
}

Any help would be appreciated, if I need to provide more info I can do that as well, as I've been stuck on this for days and I'm sure it's something small I'm missing or doing wrong.

No correct solution

OTHER TIPS

You need to ensure that the XMVECTOR & XMMATRIX types from DXMath are correctly aligned to 16-bytes (see the docs here), for stack allocations, this is automatic, but for heap allocations, you need to use _aligned_malloc to get correctly aligned memory. Alternatively you can use the XMFLOATn and XMFLOATnxn for store and use the unaligned vector loads when you actually work with them.

For classes or structs that contain XM types you can overcome this by overloading the new operator to do explicitly align allocations; the easiest is by deriving from a template such as this (note: this isn't complete, the are a few types of operator new & delete):

template <size_t align> class AlignedAllocPolicy
{
public:
    void* operator new(std::size_t size)
    {
        return _aligned_malloc(size,align);
    }

    void operator delete(void* mem)
    {
        _aligned_free(mem);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top