Archived

This topic is now archived and is closed to further replies.

Locking VB crash

This topic is 5304 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

In my mesh class, I use the following function to add vertices to the vb. This is done after the model data has been loaded from the file. The problem is that after several iterations (I use this in a for loop), the function crashes on the memcpy method.
HRESULT AddVertex(CRTMesh* pMesh, const Vec3& vVertex, const Vec3& vNormal, float tu, float tv, bool newGroup)
{
	if ( !pMesh)
		return RT_FAIL_NOVALIDMESH;

	rtMesh* mesh = pMesh->GetStruct();

    DWORD iDataSize = sizeof(RTVERTEX2);

	D3DVERTEXBUFFER_DESC desc;
	mesh->pVB->GetDesc(&desc);

	RTVERTEX2* pVertices = NULL;
	RTVERTEX2 ver;
	ver.CreateVertex (vVertex, vNormal, tu, tv);

	if (newGroup)
	{
		rtMeshGroup gr;

		gr.VertexStart = mesh->iNextVertexData;
		gr.VertexCount = 0;
		gr.bUseDetail = gr.bSphereMap = false;
		gr.Radius = 0.0f;
		gr.SphereCenter = Vec3(0,0,0);
		gr.MaxBox = gr.MinBox = Vec3(0,0,0);
		gr.pTexture = gr.pDetail = NULL;
		gr.pMaterial = NULL;
		mesh->nMaterials ++;
		mesh->groups.push_back(gr);
	}
	
	if (FAILED(mesh->pVB->Lock(mesh->iNextVertexData, iDataSize, (void**)&pVertices, D3DLOCK_NOOVERWRITE)))
		return RT_FAIL_VBLOCK;
				CopyMemory (pVertices, (void*)&ver, sizeof(RTVERTEX2));
	if (FAILED(mesh->pVB->Unlock()))
		return RT_FAIL_VBUNLOCK;

	mesh->iNextVertexData += iDataSize;
	mesh->nVertices ++;
	mesh->groups[mesh->nMaterials-1].VertexCount++;

	return RT_OK;
}
 
The vb size can hold the vertices, the pool is dynamic and as you see I am locking it every one vertex I add. I know this isnt efficient, but it shouldnt crash, right? iNextVertexData initially is zero as it should be, what am I doing wrong here? (sorry if the formatting has gone weird..)

Share this post


Link to post
Share on other sites
Bump.

I create the VB:

dev->CreateVertexBuffer (wVertexCount * sizeof(RTVERTEX2),
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, mesh->MeshFVF,
D3DPOOL_DEFAULT, &mesh->pVB, NULL);

(I am not using X files).

Share this post


Link to post
Share on other sites
*sigh*


Did you try debugging? What are the return codes on all your function calls?

My guess is that you''re going over the size limit of the buffer, that happens a lot and produces that nasty blip of a program execution.

Share this post


Link to post
Share on other sites
Found it, it wasnt entirely my fault. I was loading an MS3D file, and it is apparently using indexed tri lists for storing its data. I am using tri lists, and my vertex count needed to be 3 * tri count, whereas the MS3D vertex count was about half of the tris.

And believe me, I have tried everything, I got it working now, thanks.

Share this post


Link to post
Share on other sites