简体   繁体   English

XMMatrixTranspose 访问违规读取位置 0xFFFFFFFF

[英]XMMatrixTranspose Access violation reading location 0xFFFFFFFF

I've got a DirectX 11 Project and I'm using the Beginning DirectX 11 Game Programming book going through all the chapters.我有一个 DirectX 11 项目,我正在使用Beginning DirectX 11 Game Programming一书,贯穿所有章节。 I've got my Camera in place and it has been working with no crashes however recently when debugging my Obj Loader I've found that it keeps crashing on the XMMatrixTranspose line, I've swapped my view matrix and projection matrix round to see if it was specific to my view matrix which its not.我已经安装好我的相机并且它一直没有崩溃但是最近在调试我的 Obj Loader 时我发现它一直在XMMatrixTranspose线上崩溃,我已经交换了我的视图矩阵和投影矩阵以查看是否它特定于我的视图矩阵,它不是。

Here is the block of code that is causing the error这是导致错误的代码块

vMatrix = camera.GetViewMatrix();
vMatrix = XMMatrixTranspose(vMatrix);

pMatrix = XMMatrixPerspectiveFovLH(XM_PIDIV4, (float)(screenWidth / screenHeight), 0.01f, 100.0f);
pMatrix = XMMatrixTranspose(pMatrix);

This is found in my DX11::Render function.这可以在我的DX11::Render函数中找到。 This is called in my main.cpp game loop as seen below.这在我的main.cpp游戏循环中调用,如下所示。

MSG msg = { 0 };

while (msg.message != WM_QUIT)
{
    if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else
    {
        directX->Update(0.0f);
        directX->Render();
    }
}

directX->Shutdown();

All of my variables have memory and this code has been working before, when it started to do this it would do it very intermittently, but now it seems to be doing it all the time.我所有的变量都有内存,而且这段代码以前一直在工作,当它开始这样做时,它会非常间歇性地这样做,但现在它似乎一直在这样做。

Any help and guidance would be amazing!任何帮助和指导都会很棒! I've been researching for hours and can't find anything that works and I've had this issue for about 7 hours!我已经研究了几个小时,但找不到任何有效的方法,这个问题我已经解决了大约 7 个小时! Its gotta go!!它得走了!!

Thanks in advance.提前致谢。

UPDATE It has now gone back to intermittently crashing更新它现在已经回到间歇性崩溃

UPDATE 2 I'm also using xnamath not DXMath更新 2我也在使用 xnamath 而不是 DXMath

You should first read MSDN Programmer's Guide, specifically the type usage guidelines .您应该首先阅读 MSDN Programmer's Guide,特别是类型使用指南

DirectXMath and XNAMath are basically the same library. DirectXMath 和 XNAMath 基本上是同一个库。 XNAMath is just the old version of it which was last updated in 2011. You should move to using DirectXMath. XNAMath 只是它的旧版本,上次更新是在 2011 年。您应该转向使用 DirectXMath。 See this blog post .请参阅此博客文章

The most likely problem is that you are trying to use XMMATRIX and/or XMMVECTOR types declared on the heap (ie in a class you use new on) which will not be properly aligned for x86 (32-bit) and ARM builds.最可能的问题是您尝试使用在堆上(即在您使用new的类中)声明的XMMATRIX和/或XMMVECTOR类型,这些类型对于 x86(32 位)和 ARM 构建不会正确对齐。 It happens to line up for x64 native code, but that's mostly a happy accident.它恰好符合 x64 本机代码,但这主要是一个快乐的意外。

In other words the following code will result in an AV in x86 (32-bit) and ARM depending on exactly how memory is laid out, but will work for x64 native:换句话说,以下代码将在 x86(32 位)和 ARM 中生成 AV,具体取决于内存的布局方式,但适用于 x64 本机:

struct A
{
    XMMATRIX m;
};

A a = new A;
a->m = XMMatrixIdentity();

To make it work robustly on all architectures, you need to use:为了使其在所有架构上都能稳健运行,您需要使用:

struct A
{
    XMFLOAT4X4 m;
};

A a = new A;
XMStoreFloat4x4(&A->m, XMMatrixIdentity());

You'd use XMFLOAT4X4 or similar types which do not require alignment in your classes, and then use explicit Load/Save functions.您将使用XMFLOAT4X4或不需要在类中对齐的类似类型,然后使用显式加载/保存函数。 Note that you can also make use of more compact forms like XMFLOAT4X3 depending on your matrix content.请注意,您还可以根据矩阵内容使用更紧凑的形式,如XMFLOAT4X3

Alternatively, you can make use of the SimpleMath wrapper in the DirectX Tool Kit which does exactly that only "automagically" through the power of C++ conversion operators and constructors.或者,您可以使用DirectX 工具包中的SimpleMath包装器,它通过 C++ 转换运算符和构造函数的强大功能“自动”执行此操作。

struct A
{
    Matrix m;
};

A a = new A;
A->m = XMMatrixIdentity();
// Note this is redundant
// as the Matrix default constructor sets it to identity already.

This functionality is not part of the base library because it has some performance implications.此功能不是基础库的一部分,因为它具有一些性能影响。 By making it part of the SimpleMath wrapper instead, it allows users to opt in to an easier model of use while preventing accidental use of slower conversion paths for everyone else.通过使其成为 SimpleMath 包装器的一部分,它允许用户选择更简单的使用模型,同时防止其他人意外使用较慢的转换路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM