# Should I release vertex buffer?

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

## 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)))

void* dataVB = NULL;
{
pVB->Release();
}

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();
}

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

##### Share on other sites

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 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 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.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633716
• Total Posts
3013499
×