使用D3DXGetImageInfoFromFile()
函数给我这个:
Unhandled exception at 0x004114d4 in SAMPLE.exe: 0xC0000005: Access violation reading location 0x00000000.
以下是包含错误的代码:
// ...
WCHAR *Path = L"./LIFE.bmp";
D3DXIMAGE_INFO *Info;
IDirect3DSurface9 *Surface = NULL;
LPDIRECT3DDEVICE9 pd3dDevice;
// ...
D3DXGetImageInfoFromFile(Path, Info); // everything is fine here, unless i do the following:
pd3dDevice -> CreateOffscreenPlainSurface(Info->Width, Info->Height, Info->Format, D3DPOOL_SYSTEMMEM, &Surface, NULL);
那么,这里发生了什么?当我输入数字而不是Info->...
时,一切正常......
答案 0 :(得分:2)
您传递未初始化指针Info
,当方法尝试访问时,您将获得异常。您需要的是以下内容:
D3DXIMAGE_INFO Info;
D3DXGetImageInfoFromFile(Path, &Info);
pd3dDevice->CreateOffscreenPlainSurface(Info.Width, Info.Height, Info.Format, D3DPOOL_SYSTEMMEM, &Surface, NULL);
另外,我建议您处理返回HRESULT的任何函数的结果代码。像:
if (FAILED(D3DXGetImageInfoFromFile(Path, &Info))) {
// print something, abort or whatever.
}
如果你使用 DXUT.h ,那么V()
或V_RESULT
宏是你最好的朋友:
V(D3DXGetImageInfoFromFile(Path, &Info));
OR
HRESULT hr;
V_RETURN(D3DXGetImageInfoFromFile(Path, &Info));
V_RETURN(pd3dDevice->CreateOffscreenPlainSurface(Info.Width, Info.Height, Info.Format, D3DPOOL_SYSTEMMEM, &Surface, NULL));
V_RETURN(...);
// ... lots of D3D calls.
return S_OK;
答案 1 :(得分:0)
您可能需要转义图像路径中的反斜杠:
std::wstring wsPath = L"C:\\wood.bmp";