Visual Studio 图形调试器抛出读取访问冲突异常

Visual Studio Graphics Debugger throws read access violation exception

我正在 Visual Studio 2019 年使用 d3d11 库编写一个简单的渲染器,它构建并运行良好。但是,当我尝试 运行 图形调试器时,它会立即抛出地址 0x0000000000000000 的读取访问冲突(这显然是不正确的)。

异常是从

行的 DXCaptureReplay dll 抛出的
DeviceContext.PSSetShader(InShaderToBind.Shader.PS, NULL, 1);

其中 InShaderToBind.Shader.PS 是指向 ID3D11PixelShader 的指针

当我出于缺乏想法尝试时,它变得最奇怪

int X = 0;
ID3D11ClassInstance* FakedClassInstance = reinterpret_cast<ID3D11ClassInstance*>(&X);

DeviceContext.PSSetShader(InShaderToBind.Shader.PS, &FakedClassInstance, 1);

因为这将使异常在我尝试捕获帧之前不会抛出(我认为这是有道理的,因为该指针仅对 X 仍然有效的范围有效)

MSDN 文档指出 NULL 应该是传递给 PSSetShader 的完全有效的参数(如此处所述:https://docs.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-pssetshader

对可能出现的问题有什么想法吗?

(如果我注释掉 PSSetShader,则不会抛出异常,我可以进行捕获)

如果启用 Direct3D 调试设备,您将在调试输出中看到 window:

D3D11 CORRUPTION: ID3D11DeviceContext::PSSetShader: Second parameter (ppClassInstances) corrupt or unexpectedly NULL. [ MISCELLANEOUS CORRUPTION #14: CORRUPTED_PARAMETER2]
如果 NumClassInstances 为 0,

NULL(或者更好的 nullptr)对于 ppClassInstances 没问题。尝试:

DeviceContext.PSSetShader(InShaderToBind.Shader.PS, NULL, 0);

Generally you should make sure your program runs without emitting ERROR or CORRUPTION messages from the debug layer before attempting to use PIX or the VSGS tool.

参见 Microsoft Docs and this blog post