最近,之前运行良好的一段代码已经开始抛出异常。相关功能的相关部分:
HRESULT DC11::TryCreateDevice(
_In_ IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE driverType,
UINT createDeviceFlags,
_COM_Outptr_ ID3D11Device** ppDevice,
_Out_opt_ UINT* pShaderModel,
_COM_Outptr_ ID3D11DeviceContext** ppDeviceContext)
{
UINT createDeviceFlags0 = 0;
#ifdef _DEBUG
createDeviceFlags0 = D3D11_CREATE_DEVICE_DEBUG;
#endif
if ((createDeviceFlags & createDeviceFlags0) != 0)
{
throw std::invalid_argument(
"createDeviceFlags");
}
createDeviceFlags0 |= createDeviceFlags;
SafeComPointer<ID3D11Device> device;
SafeComPointer<ID3D11DeviceContext> deviceContext;
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
D3D_FEATURE_LEVEL featureLevel;
HRESULT hr;
hr = ::D3D11CreateDevice(
pAdapter,
driverType,
nullptr,
createDeviceFlags0,
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION,
device.GetAddressOf(),
&featureLevel,
deviceContext.GetAddressOf());
....
当我点击D3D11CreateDevice时,我得到了
Unhandled exception at 0x00007ffff54b3ce0 in CryptoNightTester.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff
这对我来说是一个棘手的指针。请记住,这部分代码在它工作和没有工作之间根本没有改变,我想知道指针参数是否出现了问题。我很快确认ppDevice和ppDeviceContext是他们应该的,所以我接着看了pAdapter。
在周围的呼叫代码是
SafeComPointer<IDXGIFactory1> factory;
CHR(CreateDXGIFactory1(
__uuidof(IDXGIFactory1),
reinterpret_cast<void**>(factory.GetAddressOf())));
SafeComPointer<IDXGIAdapter1> adapter;
HRESULT hr = factory->EnumAdapters1(
0,
adapter.GetAddressOf());
if (hr == DXGI_ERROR_NOT_FOUND)
{
return false;
}
CHR(hr);
DXGI_ADAPTER_DESC1 adapterDesc1;
hr = adapter->GetDesc1(&adapterDesc1);
FullHash mainLoopHasher;
mainLoopHasher.Create(
adapter,
5);
...
void FullHash::Create(
_Inout_ SafeComPointer<IDXGIAdapter1>& adapter,
_In_ uint32_t shaderModel)
{
this->adapter.Swap(adapter);
this->shaderModel = shaderModel;
this->SetupShader();
}
void FullHash::SetupShader()
{
UINT createDeviceFlags =
D3D11_CREATE_DEVICE_SINGLETHREADED;
bool deviceCreated = false;
#ifdef _DEBUG
// try to enable performance counters
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUGGABLE;
deviceCreated = DC11::TryCreateDevice(
this->adapter.Get(),
D3D_DRIVER_TYPE_UNKNOWN,
createDeviceFlags,
this->device.GetAddressOf(),
nullptr,
this->deviceContext.GetAddressOf()) == S_OK;
....
随后确认(据我所知,无论如何),适配器应该是它应该是什么。我测试的下一件事是当我在D3D11CreateDevice中用NULL替换pAdapter时会发生什么。它不再引发异常,但hr现在是E_INVALIDARG。
这是我有点卡住的地方。我目前最好的猜测是最近安装/卸载的软件搞砸了。也许我需要重新安装SDK或驱动程序或什么?在决定安装GPU驱动程序之前,由于我的系统非常挑剔,所以我决定在此之前发布这条路线,因此通常情况下,早期的度假胜地必须是最后的选择。
作为参考,我正在研究MSVS 2015并拥有Windows 10。
编辑:扩展了调用代码