2011-11-07 17 views
3

我不知道的使用具有一個參數表示爲雙指針函數裏面的但是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調用。

他們是OK的嗎?

在此先感謝

+0

看起來好像沒什麼問題。 –

回答

3

這是確定的,但可以簡化爲:

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是必要的,因爲pSampleRelease當它超出範圍。

0

分配和取消引用爲AddRef()是正確的。

MFCreateVideoSampleFromSurface()被調用時,第二個參數是在指針到接口應當被存儲的位置。您使用&pSample來獲取傳遞給該函數的地址。這匹配所需的IMFSample **類型。請注意,通過CComPtrBase<>CComPtr<>上的&運算符返回正確的類型。

CComPtrBase::& operator @ MSDN

ppVideoSample是類型IMFSample **爲好,這需要操作員*取消引用接口指針。這產生IMFSample *類型,你可以用->操作者調用訪問AddRef()等功能界面上的指針。

3

的更地道的版本將是

// Transfer the pointer to our caller. 
*ppVideoSample = pSample.Detach(); 

如果你想複製的語義,而不是轉移,你可以使用

pSample.CopyTo(ppVideoSample);