Getting run time error when moving variable to header file

220 views Asked by At

I'm working on a DirectX application, and I'm having some trouble multiplying 2 matrices together, when i try to I get a run time error. I only get the error if i declared the matrices in my header, if i declare them locally in the function it works fine. This isn't the only place in the application where i get the this type of error.


GraphicClass.cpp

void GraphicClass::UpdateScene()
{
    Cube1World = DirectX::XMMatrixIdentity();

    DirectX::XMVECTOR rotaxis = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
    Rotation = DirectX::XMMatrixRotationAxis(rotaxis, rot);
    Translation = DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f);

    Cube1World = Translation * Rotation; //I get run time error on this line
}

GraphicClass.h

class GraphicClass{

    DirectX::XMMATRIX Cube1World;
    DirectX::XMMATRIX Rotation;
    DirectX::XMMATRIX Translation;
    float rot = 0.01f;

    //other Code
}

Error Message

Unhandled exception at 0x002F4C0F in Engine.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF.

I'm able to declare Cube1World in the header file, but i if try to do it with either Rotation or Translation i get the run time error.

I'm running windows 8.1 with visual studio 2015 and the latest directx sdk. I hope i explained the problem well enough, thanks in advance.

2

There are 2 answers

1
Sumsar On BEST ANSWER

I found the answer on this thread

Crash after m = XMMatrixIdentity() - aligment memory in classes?

"In the Book "Introduction to 3D Game Programming with Directx 11" from "Frank D. Luna" it says:

Do not use XMMATRIX as Member of a class or Structure. Always use XMFloat4x4 and load and store back if you need it. "

2
Juan Marcos Mervi On

Try to define a Constructor and then initialize your data inside. To avoid multiple include define include guards in the *.h file or use #pragma once.