Jump to content
  • Advertisement
Sign in to follow this  
anders211

Should I release vertex buffer?

This topic is 1003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

I wonder if in the below function vertex buffer should be released or not?

The aim of function is to extend vertex buffer by adding to vertices,normal,uv coordinates also bone indices and weights.

I checked if mesh is drawing after pVB release and it is drawn. So it seems I loose nothing from my mesh when I release pVB. Could anyone confirm that I have a right and pVB shoould be released at the end of function, because otherwise there will be memore leak?

void SkinnedMesh::addToMeshBoneIndicesAndWeights4(ID3DXMesh* mesh, const vector< vector<u32> >& boneIndices, const vector< vector<float> >& weights)
{
IDirect3DVertexBuffer9 *pVB = NULL;


if(FAILED(mesh->GetVertexBuffer(&pVB)))
    throw runtime_error("SkinnedMesh::addToMeshBoneIndicesAndWeights4::GetVertexBuffer");


void* dataVB = NULL;
if(FAILED(pVB->Lock(0, 0, &dataVB, D3DLOCK_DISCARD))) 
{
    pVB->Release();
    throw runtime_error("SkinnedMesh::addToMeshBoneIndicesAndWeights4::Lock");
}


float* data = (float *) dataVB;
for(u32 i=0; i<mesh->GetNumVertices(); ++i)
{       
    data[12*i+8] = weights.at(i).at(0);
    data[12*i+9] = weights.at(i).at(1);
    data[12*i+10] = weights.at(i).at(2);
    u32 x = (boneIndices.at(i).at(0) & 0xFF) | 
     ((boneIndices.at(i).at(1) << 8 )& 0xFF00) |
     ((boneIndices.at(i).at(2) << 16) & 0xFF0000) |
     ((boneIndices.at(i).at(3) << 24) & 0xFF000000);
    memcpy(&data[12*i+11], &x, 4);
}


if(FAILED(pVB->Unlock()))
{
    pVB->Release();
    throw runtime_error("SkinnedMesh::addToMeshBoneIndicesAndWeights4::Unlock");
}


pVB->Release(); //?????
}
Edited by anders211

Share this post


Link to post
Share on other sites
Advertisement

COM objects are ref-counted, and the general convention is that retrieving a pointer to a COM interface increments the ref count on the object it points to. So yes, when you're done using it, you need to call Release on the vertex buffer returned from GetVertexBuffer to decrement the ref count. Otherwise you'll have a memory leak.

Share this post


Link to post
Share on other sites

You are using exceptions.

I think it's better to wrap the pointer with something like Microsoft::WRL::ComPtr<>

In this case you may not to worry about manual adding Release() in all code paths.

Share this post


Link to post
Share on other sites

There is absolutely no reason why you would not release the memory after you use it in this scenerio. You are performing a deep copy so there is no need to hold a reference to the buffer anymore, when you are done with it, release it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!