locked
Releasing IBufferByteAccess

    Question

  • There's plenty of code on these forums that show how to use IBuffer/IBufferByteAccess to get the pixel data from a WriteableBitmap. In the code below the IUnknown instance is released. However, shouldn't the IBufferByteAccess instance also be released, in order to avoid a memory leak?

        IUnknown* pUnknown = reinterpret_cast<IUnknown*>(buffer);
        IBufferByteAccess* pBufferByteAccess = nullptr;
        HRESULT hr = pUnknown->QueryInterface(IID_PPV_ARGS(&pBufferByteAccess));
        pUnknown->Release();
        byte *pPixels = nullptr;
        hr = pBufferByteAccess->Buffer(&pPixels);

    Dave

    Thursday, June 14, 2012 9:33 PM

Answers

  • Hi Dave,

    Since you get IBufferByteAccess via QueryInterface you need to Release it when you are done with it. That call isn't shown in the code snippet since IBufferByteAccess may still be in use.

    --Rob

    • Marked as answer by David Britch Monday, June 18, 2012 11:47 PM
    Monday, June 18, 2012 10:52 PM
    Owner
  • I think the solution to all this is to Release the IBufferByteAccess rather than the IUnknown, and use smart pointers to encapsulate it all.

    Dave

    • Marked as answer by David Britch Monday, June 18, 2012 11:48 PM
    Monday, June 18, 2012 11:48 PM

All replies

  • Hello,

     

    I think, if you use QueyrInterface to create IBufferByteAccess you need to release by yourself.

     

    But if you use ref new to create, you needn't to call release to free.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 18, 2012 11:27 AM
  • In the code above I've called QueryInterface on the IUnknown instance and then called Release on it. However, I'm also wondering if I need to call Release on the IBufferByteAccess instance, on which I've neither called QueryInterface or used ref new to create it.


    Dave

    Monday, June 18, 2012 7:08 PM
  • Hi Dave,

    Since you get IBufferByteAccess via QueryInterface you need to Release it when you are done with it. That call isn't shown in the code snippet since IBufferByteAccess may still be in use.

    --Rob

    • Marked as answer by David Britch Monday, June 18, 2012 11:47 PM
    Monday, June 18, 2012 10:52 PM
    Owner
  • I think the solution to all this is to Release the IBufferByteAccess rather than the IUnknown, and use smart pointers to encapsulate it all.

    Dave

    • Marked as answer by David Britch Monday, June 18, 2012 11:48 PM
    Monday, June 18, 2012 11:48 PM