Sign in to follow this  

Index buffer access violation

Recommended Posts

I don't know why this is giving me an access violation but it is. I initialize the VB and IB like this, making a simple box:
Vertex BoxVertices[8];
	BoxVertices[0].x = _Box.min.x; BoxVertices[0].y = _Box.min.y; BoxVertices[0].z = _Box.min.z;
	BoxVertices[1].x = _Box.min.x; BoxVertices[1].y = _Box.max.y; BoxVertices[1].z = _Box.min.z;
	BoxVertices[2].x = _Box.max.x; BoxVertices[2].y = _Box.max.y; BoxVertices[2].z = _Box.min.z;
	BoxVertices[3].x = _Box.max.x; BoxVertices[3].y = _Box.min.y; BoxVertices[3].z = _Box.min.z;
	BoxVertices[4].x = _Box.min.x; BoxVertices[4].y = _Box.min.y; BoxVertices[4].z = _Box.max.z;
	BoxVertices[5].x = _Box.min.x; BoxVertices[5].y = _Box.max.y; BoxVertices[5].z = _Box.max.z;
	BoxVertices[6].x = _Box.max.x; BoxVertices[6].y = _Box.max.y; BoxVertices[6].z = _Box.max.z;
	BoxVertices[7].x = _Box.max.x; BoxVertices[7].y = _Box.min.y; BoxVertices[7].z = _Box.max.z;

	short BoxIndex[] = { 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 };

	D3DDevice->CreateVertexBuffer(sizeof(BoxVertices), 0, FVF_FORMAT, D3DPOOL_MANAGED, &bbvb, NULL);
	VOID* pVertex;
	bbvb->Lock(0, 0, (void**)&pVertex, 0);
	memcpy(pVertex, BoxVertices, sizeof(BoxVertices));

	D3DDevice->CreateIndexBuffer(sizeof(BoxIndex), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &bbib, NULL);
	VOID* pShort;
	bbib->Lock(0, 0, (void**)&pShort, 0);
	memcpy(pShort, BoxIndex, sizeof(BoxIndex));
then I draw it like this:
if(DrawBoundingBox) {
		D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
		D3DDevice->SetStreamSource(0, bbvb, 0, sizeof(Vertex));
		D3DDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 8, 0, 24);
		D3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
It doesn't always give me an access violation, but I can't work out what I do that causes the access violation.

Share this post

Link to post
Share on other sites
You aren't checking the return codes for your lock calls - how do you know that the returned buffer is even valid for locking? Sure, it's very likely to be fine but it is NOT guaranteed. Blindly assuming that calls succeed is a good way of causing these sorts of bugs.

Also, if it's Visual Studio or the MSVCRT giving you the error you can usually step-through the code to find the line that it doesn't like and work backwards from there.


Share this post

Link to post
Share on other sites
Ok, I've added the checks for if it's succeeding or not but I still get the access violations.

It's this line that it's telling me:
D3DDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 8, 0, 24);

I noticed that I get some lines and dots in the top left corner when the lines are being drawn.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this