其他分享
首页 > 其他分享> > c – 双指针函数参数和CComPtr

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超出范围时,它将释放.

标签:atl,c,smart-pointers
来源: https://codeday.me/bug/20190826/1732676.html