How to release WriteableBitmap memory?



    Perhaps someone could help with this issue. Please read the code behind:



    void myapp::MainPage::Invalidate()
    	/* Get a new bitmap*/
      	m_pDIBitmap = m_pViewer->GetRenderData(FALSE);
     	m_pWriteableBitmap =ref new WriteableBitmap(m_pDIBitmap->GetWidth(), m_pDIBitmap->GetHeight());
      	IBuffer^ buffer = m_pWriteableBitmap->PixelBuffer;
      	/* Take the WinRT IBuffer interface and cast to an IUnknown */
      	IUnknown* pUnk = reinterpret_cast<IUnknown*>(buffer);
     	IBufferInternal* pBufferInternal = nullptr;
     	/* Get the undocumented interface for accessing the pixel buffer */
      	HRESULT hr = pUnk->QueryInterface(IID_IBufferInternal, 
    	hr = pBufferInternal->GetBuffer(&m_pWriteableBitmapBuffer);
     	memcpy(m_pWriteableBitmapBuffer, m_pDIBitmap->GetBuffer(),  m_pDIBitmap->GetHeight()*m_pDIBitmap->GetPitch());
    	/* Invalidates the writeable bitmap so it updates */
    	/* Set the <Image/> source in the xaml to our WriteableBitmap */
    	Img->Source = m_pWriteableBitmap;

    During run these codes,  the memory consumption increments rapidly and the memory is never freed.

    Does anyone has any solution or workaround for this problem? Do i miss anything?

    Thanks in advance.

    • Edited by frglig Thursday, January 19, 2012 2:33 AM more details a bout the question
    Thursday, January 19, 2012 2:00 AM


  • If you are incrementing a reference count yourself by explicitly calling QueryInterface, you need to call Release on the returned interface pointer as well.




    • Marked as answer by Bob_Bao Tuesday, February 07, 2012 6:42 AM
    Tuesday, January 24, 2012 4:22 PM