0

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.

Sumsar
  • 135
  • 1
  • 9
  • 1. 0xFFFFFFFF looks a lot like an uninitialized or smashed pointer. 2. Distil the problem down to the minimum possible, probably the header, a chopped down version of the class with the required members and the UpdateScene method, and a main function that calls UpdateScene. – user4581301 Jun 23 '15 at 20:10
  • If you step through the code with VS's debugger, what do Rotation and Translation look like before the crash? – user4581301 Jun 23 '15 at 20:33
  • An interesting bit of devious deviance that is. – user4581301 Jun 23 '15 at 21:15

2 Answers2

3

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.

  • 1
    Reasonable advice, but doesn't explain how it relates to, or if it is even relevant to, the OP's problem . – user4581301 Jun 23 '15 at 20:20
  • I made a Constructor and initialize my data, but it didn't fix the problem GraphicClass::GraphicClass() { Translation = DirectX::XMMATRIX(); Rotation = DirectX::XMMATRIX(); } – Sumsar Jun 23 '15 at 20:30
1

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. "

Community
  • 1
  • 1
Sumsar
  • 135
  • 1
  • 9
  • Read the [DirectXMath Programmer's Guide](https://msdn.microsoft.com/en-us/library/windows/desktop/ee415571.aspx) specifically the section Getting Started, [Type Usage Guidelines](https://msdn.microsoft.com/en-us/library/windows/desktop/ee418725.aspx#type_usage_guidelines_). Note that you might want to use the [SimpleMath](http://blogs.msdn.com/b/shawnhar/archive/2013/01/08/simplemath-a-simplified-wrapper-for-directxmath.aspx) wrapper in [DirectX Tool Kit](http://go.microsoft.com/fwlink/?LinkId=248929) which makes using the types a bit more noobie friendly. – Chuck Walbourn Jun 24 '15 at 03:09