可以从函数返回CComPtr吗?

时间:2018-04-16 01:53:04

标签: com atl ms-media-foundation

我正在编写一个创建 IMFSample 的函数并为其添加缓冲区。我想将新的 CComPtr 返回到 IMFSample ,而不是将指针传递给参数中的指针。这样做可以吗?当函数退出时, IMFSample 的引用计数是否正确?我已经研究过,并且无法找到有关此用法的任何示例或讨论。以下是该函数的简化示例:

CComPtr<IMFSample> getSample() {
    HRESULT hr = S_OK;
    CComPtr<IMFSample> pSample = NULL;

    hr = MFCreateSample(&pSample);
    if (hr != S_OK) {
        // pSample should be released at exception.
        throw "MFCreateSample failed";
    }

    // ...Create buffer, Add buffer, etc...

    return pSample;
}

在这里,我添加了一个更长的版本,以帮助展示如何通过使用异常简化我的错误处理。我的程序的其余部分使用异常结构,所以我试图隔离&#34; COM&#34;部件,以使他们尽可能地与其余部分更好地配合。

void decodeLoop() {
    CComPtr<IMFSample> pSample;

    while (noError) {
        try {
            pSample = getNALSample(decoderPkt, MAX_SIZE);
            processSample(pSample);
            pSample = NULL;
            ...
        }
        catch (MyException& e) {
            ...
        }
    }
}

CComPtr<IMFSample> getNALSample(DecoderPkt* decoderPkt, DWORD maxBuffSize) {
    HRESULT hr = S_OK;
    CComPtr<IMFSample> pSample = nullptr;
    CComPtr<IMFMediaBuffer> pMediaBuffer = nullptr;
    byte *pBuffer = nullptr;

    // MFCreate Sample
    hr = MFCreateSample(&pSample);
    if (hr != S_OK) 
        throw MyException(hr, "getNALSample::MFCreateSample failed");

    // Get buffer
    hr = MFCreateMemoryBuffer(maxBuffSize, &pMediaBuffer);
    if (hr != S_OK) 
        throw MyException(hr, "getNALSample::MFCreateMemoryBuffer failed");

    // Set up buffer pointer
    hr = pMediaBuffer->Lock(&pBuffer, nullptr, nullptr);
    if (hr != S_OK) 
        throw MyException(hr, "getNALSample::pMediaBuffer->Lock failed");

    // Build NAL from decoder que
    hr = pMediaBuffer->SetCurrentLength(buildNAL(pBuffer, decoderPkt));
    if (hr != S_OK) 
        throw MyException(hr, "getNALSample::pMediaBuffer->SetCurrentLength failed");

    hr = pMediaBuffer->Unlock();
    if (hr != S_OK) 
        throw MyException(hr, "getNALSample::pMediaBuffer->Unlock failed");

    // Add buffer to sample
    hr = pSample->AddBuffer(pMediaBuffer);
    if (hr != S_OK) 
        throw MyException(hr, "getNALSample::pSample->AddBuffer failed");

    return pSample;
}

谢谢。

1 个答案:

答案 0 :(得分:2)

工作正常。

你之所以没有看到这种用法的原因,是因为它的设计方式,它看起来像一个真正的私人&#34;方法

另外它抛出而不是返回HRESULT。就个人而言,我总是喜欢返回HRESULT而不是扔(即使你带着ComPtr),因为它更容易处理。

这是一种(更加复杂,我承认)这样做的方式,看起来更多&#34; COM&#34; (或更多&#34;微软&#34;如果你愿意的话):

HRESULT GetSample(REFIID riid, void**ppv) // you can add other parameters (in front, and leave riid and ppv last)
{
  HRESULT hr = S_OK;
  CComPtr<IMFSample> pSample;

  hr = MFCreateSample(&pSample);
  if (hr != S_OK) return hr; // or if (FAILED(hr) ? depends if you handle S_FALSE an other non error cases

  // ...Create buffer, Add buffer, etc...

  return pSample->QueryInterface(riid, ppv);
}