c – 双指针函数参数和CComPtr

我不确定在一个函数中使用CComPtr的方法,该函数有一个表示为双指针的参数:

HRESULT D3DPresentEngine::CreateD3DSample(
    IDirect3DSwapChain9 *pSwapChain, 
    IMFSample **ppVideoSample
    )
{
    // Caller holds the object lock.

    D3DCOLOR clrBlack = D3DCOLOR_ARGB(0xFF, 0x00, 0x00, 0x00);

    CComPtr< IDirect3DSurface9 > pSurface;
    CComPtr< IMFSample > pSample;

    // Get the back buffer surface.
    ReturnIfFail( pSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurface ) );

    // Fill it with black.
    ReturnIfFail( m_pDevice->ColorFill(pSurface, NULL, clrBlack));

    // Create the sample.
    ReturnIfFail( MFCreateVideoSampleFromSurface(pSurface, &pSample));

    // Return the pointer to the caller.
    *ppVideoSample = pSample;
    (*ppVideoSample)->AddRef();

    return S_OK;
}

我对最后一次分配AddRef调用有疑问.

他们对你好吗?

提前致谢

解决方法:

没关系,但可以简化:

HRESULT D3DPresentEngine::CreateD3DSample(
    IDirect3DSwapChain9 *pSwapChain, 
    IMFSample **ppVideoSample
    )
{
    // Caller holds the object lock.

    D3DCOLOR clrBlack = D3DCOLOR_ARGB(0xFF, 0x00, 0x00, 0x00);

    CComPtr< IDirect3DSurface9 > pSurface;

    // Get the back buffer surface.
    ReturnIfFail( pSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurface ) );

    // Fill it with black.
    ReturnIfFail( m_pDevice->ColorFill(pSurface, NULL, clrBlack));

    // Create the sample.
    ReturnIfFail( MFCreateVideoSampleFromSurface(pSurface, ppVideoSample));

    return S_OK;
}

在您的代码中,AddRef是必需的,因为当PSample超出范围时,它将释放.

上一篇:c – 强制资源从非mfc app中的dll加载的WTL方式? (我们使用的是WTL / ATL,而不是直接的win32)


下一篇:MFC ATL STL概要