• Error Checking in DX


    from http://nexe.gamedev.net/directKnowledge/default.asp?p=Debugging

    When you're trying to find out why a specific function is failing - and the debug output isn't helping - you need to work with return codes. Note, however, that you should always check error codes, not just when debug output isn't useful. More or less all the functions in the DirectX API return HRESULT?s, the standard COM return type. Given an HRESULT? hr, you can check for success and failure using the following macros:
    - SUCCEEDED(hr) will be true if hr indicates an 'ok', that is, the function you got the result from succeeded. We stress here that you should never compare an HRESULT? to S_OK or whatever to check for success, because success can be indicated by many values, not just S_OK.
    - FAILED(hr) will be true if hr indicates a failure.
    Additionally, the following functions and macros are helpful as well:
    - DXGetErrorString9(hr) will return a string which is the name of the constant that hr represents. If hr were D3DERR_DEVICELOST, the string would be "D3DERR_DEVICELOST".
    - DXGetErrorDescription9(hr) will return a string which describes the error code in hr. If hr were D3DERR_DEVICELOST, the string would be something like "The device has been lost but cannot be reset at this time. Therefore, rendering is not possible".
    - DXTRACE_ERR("function�failed:�",�hr) will output - to the debug view - the current source file, line number, the string "function failed: " and the string produced by DXGetErrorString9(hr)
    - DXTRACE_ERR_MSGBOX("function�failed:�",�hr) does exactly the same thing as DXTRACE_ERR, except that it pops up a dialog box with the error, as well as printing it to the debug output.
    - DXTRACE_MSG("hello") is the same as writing DXTRACE_ERR("hello",�0). (It doesn't use hr at all but we're including it for completeness).
    You can also feed the error code to the DirectX Error Lookup tool in the DirectX SDK to get a description.
    When you're working with DirectX types in the debugger, you will probably find it useful to read In|Framez's article on auto-expand information for DirectX9. It'll make the debugger a lot more 'intelligent' when working with DirectX.

    all the Macros above were can be found in DxErr.h

    an example about this, the following code show the error message on a box

    const WCHAR* errorString = DXGetErrorString(hr) ;
    DXTRACE_ERR_MSGBOX(errorString, hr) ;

    注意有些函数只能在DEBUG模式下使用

    代码
    #if defined(DEBUG) | defined(_DEBUG)
    #define DXTRACE_MSG(str) DXTrace( __FILE__, (DWORD)__LINE__, 0, str, FALSE )
    #define DXTRACE_ERR(str,hr) DXTrace( __FILE__, (DWORD)__LINE__, hr, str, FALSE )
    #define DXTRACE_ERR_MSGBOX(str,hr) DXTrace( __FILE__, (DWORD)__LINE__, hr, str, TRUE )
  • 相关阅读:
    07.消除过期对象的引用
    1.1进程和多线程概述
    1.2什么是操作系统
    06.避免创建不必要的对象
    05.依赖注入优先于硬连接资源
    04.使用私有构造器执行非实例化
    03.使用私有构造方法或枚类实现 Singleton 属性
    02.当构造参数过多时使用builder模式
    01.考虑使用静态工厂方法替代构造方法
    iiS申请地址
  • 原文地址:https://www.cnblogs.com/graphics/p/1739757.html
Copyright © 2020-2023  润新知