locked
How to release vertex buffer and index buffer by C++/CX ( XAML+DirectX )?

    Question

  •  I create vertex buffer and index buffer in WinRT app by C++/CX.

    So, How to release it ?

    I want to do it because the memory always increase  when I open a new file.

    Below my Code:

     Microsoft::WRL::ComPtr<ID3D11Buffer>    m_vertexBuffer;
     Microsoft::WRL::ComPtr<ID3D11Buffer>    m_indexBuffer;

    const CD3D11_BUFFER_DESC vertexBufferDesc = CD3D11_BUFFER_DESC(  sizeof(float)*MeshData.size() , D3D11_BIND_VERTEX_BUFFER ); 
     // Create the vertex buffer. 
     D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
     vertexBufferData.pSysMem = &MeshData[0];
     vertexBufferData.SysMemPitch = 0;
     vertexBufferData.SysMemSlicePitch = 0;
     DX::ThrowIfFailed
      (
      m_d3dDevice->CreateBuffer
      (
      &vertexBufferDesc,
      &vertexBufferData,
      &m_vertexBuffer
      )
      );

     // Create the index buffer.
     D3D11_SUBRESOURCE_DATA indexBufferData = {0};
     indexBufferData.pSysMem = &ConstIndex[0];
     indexBufferData.SysMemPitch = 0;
     indexBufferData.SysMemSlicePitch = 0;
     const CD3D11_BUFFER_DESC indexBufferDesc = CD3D11_BUFFER_DESC( sizeof(unsigned short int)*ConstIndex.size() , D3D11_BIND_INDEX_BUFFER );

    Wednesday, July 16, 2014 3:54 AM

Answers

All replies

  • m_vertexBuffer.Reset() and  m_indexBuffer.Reset().

    This assumes you have only a single reference to each buffer of course, which is not necessarily true.

    See http://msdn.microsoft.com/en-us/library/windows/desktop/ff485846.aspx

    http://msdn.microsoft.com/en-us/library/windows/desktop/bb205070.aspx

    Wednesday, July 16, 2014 6:10 AM
  • Thank you for help really.

    I found the problem is still exist.

    My MeshData size is about 30 MB.

    PS:const CD3D11_BUFFER_DESC vertexBufferDesc = CD3D11_BUFFER_DESC(  sizeof(float)*MeshData.size() , D3D11_BIND_VERTEX_BUFFER ); 

    When I opened the same file again, the memory increase about 30MB.

    So, I release failure ?

    And I need to release below code ?

    // Set vertex buffer. 
    UINT stride = sizeof( VertexPositionColor ); //UINT: unsigned 
    UINT offset = 0;
    m_d3dContext->IASetVertexBuffers
    (
    0,
    1,
    m_vertexBuffer.GetAddressOf(),
    &stride,
    &offset
    );


    • Edited by KC-Steven Thursday, July 17, 2014 9:10 AM
    Thursday, July 17, 2014 9:01 AM
  • Ah, I see the problem. Change

    m_vertexBuffer.GetAddressOf(),

    to

    m_vertexBuffer.ReleaseAndGetAddressOf(),

    BTW, are you keeping the MeshData object around in memory? Once you create your IB and VB, you can release the 30 MB of MeshData object since the  IB/VB makes a copy.

    Thursday, July 17, 2014 6:09 PM