http://www.toymaker.info/Games/html/directx_q_a.html
I have a DirectX related bug I cannot track down!
The first thing to do is to check the debug output in Viz, DirectX puts warnings and errors there that can help you find a problem. This requires you have DirectX in debug mode (it can be changed via the control panel - the number of warnings you get is controlled by the slider). In addition in the latest versions of the SDK you need to defineD3D_DEBUG_INFO to get full messages (put it in project-settings preprocessor definitions).
DirectX provides a means of getting a DirectX error string. Each DirectX function returns a HRESULT, you can pass that to an error function to get an error string and then output it to the debug pane or the window. Note: OutputDebugStringsends output to the debug pane in Viz.
The two functions are DXGetErrorString(hr),DXGetErrorDescription(hr)
Note: these functions replace the older DXGetErrorString9 and DXGetErrorDescription9 functions. Also the files are renamed from Dxerr9.h and the link file Dxerr9.lib to Dxerr.h and Dxerr.lib
To use these functions you must include Dxerr.h and link to Dxerr.lib (put it in the project - settings - link - object / library modules text box).
e.g.
HRESULT hr;
hr=D3DXCreateTextureFromFile( gDevice, d3dxMaterials[i].pTextureFilename, &m_meshTextures[i] );
if (FAILED(hr))
{
char buf[2048];
sprintf(buf, "Error: %s error description: %s\n",DXGetErrorString(hr),DXGetErrorDescription(hr));
OutputDebugString(buf);
}
Note: rather than using the C-style printf code above you should prefer to use C++ Strings.
There are also functions in Dxerr.h that will display the error for you without having to do the above. Simply call:
DXTRACE_ERR(TEXT("My error description"),hr)
So the above example could now be written more neatly as:
if (FAILED(hr=D3DXCreateTextureFromFile( gDevice, d3dxMaterials[i].pTextureFilename, &m_meshTextures[i] )))
DXTRACE_ERR(TEXT("Failed to create texture"),hr);
Since you are constantly having to check the HRESULT from DirectX calls it makes sense to write one error function to handle them all rather than repeating the above each time. I have a class that is full of static helper functions which includes a function to check if a hr value is failed. If you create a macro you can even pass in the file and line number that the problem occurred on. e.g.
#define CheckHr(hr) CheckForDxError(__FILE__,__LINE__,hr)
The file and line are provided by the compiler and allow you to output where the problem occurred. In addition if you output them correctly you can double click on the error in the output pane of Viz and be taken to the line in question. Your function may then look something like this:
void CheckForDxError(const char *file, int line, HRESULT hr)
{
if (!FAILED(hr))
return;
// Get the direct X error and description
char desc[1024];
sprintf(desc,"(DX) %s - %s",DXGetErrorString(hr),DXGetErrorDescription(hr));
// Output the file and line number in the correct format + the above DX error
char buf[2048];
sprintf(buf,"%s(%d) : Error: %s\n", file, line, desc);
OutputDebugString(buf);
// Cause the debugger to break here so we can fix the problem
DebugBreak();
}
Also see the answer below:
How do I use a watch on Direct3D Interfaces?
If you try to use the watch window to find out more about the Direct3D interfaces you will likely see lots of rubbish. To allow you to drill down the interfaces (with the loss of some speed - so just enable in debug mode) define this before including d3d9.h :
#define D3D_DEBUG_INFO
I get memory leak errors from DirectX when my application closes, how do I get rid of them?
Every DirectX object you have obtained must be released before your application closes. This can include the Direct3D object, the device, textures, vertex buffers, index buffers, state blocks, sprites, fonts etc. Luckily every object implements the COM IUnknown set of interfaces which include a Release() function, so the process is the same to release everything e.g.
device->Release()
You must release things in the correct order. So the device and object are released last.
Advanced: If you have searched and searched and still cannot find what it is that you have failed to release there is one further, advanced, trick that can help. You will have some sort of leak message like:
Direct3D9: (ERROR) :Memory Address: 003b4b3c lAllocID=1 dwSize=00003524,ReturnAddr=00cb7346 (pid=00000108)
Go to the DirectX properties dialog box in control panel and make sure it is in debug mode. Then in the 'debugging box', in the part which says 'brake on AllocID' fill in the IAllocId in your error message. Run the game and you should then see which items are not releasing. The IAllocID of 1 is probably the main DirectX object and is not being released due to something later so check against the higher id numbers first.
本文介绍如何使用DirectX的调试工具定位并解决运行时出现的问题,包括启用调试模式、利用DXGetErrorString等函数获取错误信息的方法,并提供了一个实用的错误检查函数示例。
1万+

被折叠的 条评论
为什么被折叠?



