D3D 和 D3D 的问题通讯

发布于 2024-08-23 09:08:33 字数 662 浏览 7 评论 0原文

所有的D3D接口都是从COM的IUnknown接口派生的,所以我想我会采取一种简单的方法来释放D3D对象并使用类似这样的东西:

__inline BOOL SafeRelease(IUnknown*& pUnknown)
{
    if(pUnknown != NULL && FAILED(pUnknown->Release()))
        return FALSE;

    pUnknown = NULL;
    return TRUE;
}

但这不起作用,因为编译器将生成无效的类型转换错误,当我尝试使用它时。我能想到的唯一解决办法是:

__inline BOOL SafeRelease(void* pObject)
{
    IUnknown* pUnknown = static_cast<IUnknown*>pObject;
    if(pUnknown != NULL && FAILED(pUnknown->Release()))
        return FALSE;

    return TRUE;
} 

但后来我失去了一些功能,而且它看起来(而且确实)非常狡猾。有更好的方法吗?像我的第一个例子一样工作的东西将是最佳的,尽管我想避免使用任何宏(如果可能的话)

all the D3D interfaces are derived from COM's IUnknown interface, so I though I'd take an easy route for releasing D3D objects and use something like this:

__inline BOOL SafeRelease(IUnknown*& pUnknown)
{
    if(pUnknown != NULL && FAILED(pUnknown->Release()))
        return FALSE;

    pUnknown = NULL;
    return TRUE;
}

this doesn't work though, as the compiler will generate invalid type conversion error(s), when I try to use it. the only way around it I could think of was this:

__inline BOOL SafeRelease(void* pObject)
{
    IUnknown* pUnknown = static_cast<IUnknown*>pObject;
    if(pUnknown != NULL && FAILED(pUnknown->Release()))
        return FALSE;

    return TRUE;
} 

but then I loose some functionality and it also looks(and is) very dodgy. is there a better way to do this? something that works like my first example would be optimal, though I would like to avoid using any macros(if its possible)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风为裳 2024-08-30 09:08:33

处理 COM 资源的常用方法是使用 RAII 并让 ATL 等辅助类 CComPtrCComQIPtr 尽可能为您处理引用计数。

void f() {
    CComPtr<IDirect3DDevice> sp3dDev;
    getDevice(&sp3dDev);
    // ... do stuff
} // smart pointer gets destroyed, calls Release() if valid

The commonly taken route for dealing with COM resources is to employ RAII and let helper classes like ATLs CComPtr or CComQIPtr handle reference counting for you as far as possible.

void f() {
    CComPtr<IDirect3DDevice> sp3dDev;
    getDevice(&sp3dDev);
    // ... do stuff
} // smart pointer gets destroyed, calls Release() if valid
温折酒 2024-08-30 09:08:33

模板函数可以解决您的问题:

template<class T>
__inline bool SafeRelease(T*& pUnknown)
{
    if (pUnknown == NULL) return false;
    if (0 == pUnknown->Release()) 
        pUnknown = NULL;
    return true;
}

A template function solves your problem:

template<class T>
__inline bool SafeRelease(T*& pUnknown)
{
    if (pUnknown == NULL) return false;
    if (0 == pUnknown->Release()) 
        pUnknown = NULL;
    return true;
}
蓝梦月影 2024-08-30 09:08:33

如果您可以执行最初编写的操作,那么您将违反类型安全性:

IDirect3DDevice *blah;
IUnknown *bar;
IUnknown *&unknown= blah;
unknown= bar;

显然,将 bar 分配给 unknown 将意味着 blah 指向一个IUnknown,这会违反类型安全。

nobugz 的解决方案可能是你想要做的,尽管我认为设置那些指向 NULL 的指针不会提高代码的质量。如果您需要确保其他代码不会多次Release,您可能应该修复该代码,而不是让有错误的代码不会失败。

If you could do what you originally wrote, then you would violate type safety:

IDirect3DDevice *blah;
IUnknown *bar;
IUnknown *&unknown= blah;
unknown= bar;

Clearly, assigning bar to unknown would mean that blah points to an IUnknown, which would violate type safety.

nobugz's solution is probably what you want to do, although I would argue that setting those pointers to NULL doesn't increase the quality of your code. If you need to ensure that other code won't Release multiple times, you probably should go fix that code rather than making buggy code not fail.

伪心 2024-08-30 09:08:33

我有一段时间没有使用 DirectX,但我记得它的标头中的某处有一个 SAFE_RELEASE 宏。 Google 代码搜索显示它位于 dxutil.h 中。

I haven't worked with DirectX in a while but I remember it has a SAFE_RELEASE macro somewhere in it's headers. Google code search shows it's in dxutil.h.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文