Sign in to follow this  
Unliterate

What in the world.. (Direct X related)

Recommended Posts

Not a very well defined topic header but the problem hasnt really well defined itself either.. here's the situation: I'm trying to render some polys to a screen using DX9 but I'm getting inconsitant and random memory read errors, sometimes (but rarely) none at ALL. I have a load vertices function:
void DISPLAY::LoadVertexData(D3DXVECTOR2 *coordinates,D3DCOLOR *colors,int number_of_coords)
{
	VOID* pVertices;
	if(FAILED(D3DVertexBuffer->Lock(0,0,(void**) &pVertices,D3DLOCK_DISCARD/*entire buffer is overwritten*/)))
	{
		MessageBox(window,"Failed to lock the vertex buffer!","Damn!",MB_ICONEXCLAMATION | MB_OK);
		return;
	}

	int index=0;
	while(index<number_of_coords)
	{
		screen_vertices[index].x=coordinates[index].x;//coordinates[index].x;
		screen_vertices[index].y=coordinates[index].y;
		screen_vertices[index].z=0.5f;
		screen_vertices[index].rhw=1.0f;
		screen_vertices[index].color=colors[index];
		index++;
	}

	memcpy(pVertices,screen_vertices,sizeof(/*CUSTOM_VERTEX*/screen_vertices)/**number_of_coords*/);

	if(FAILED(D3DVertexBuffer->Unlock()))
		MessageBox(window,"Failed to unlock the vertex buffer!","Damn!",MB_ICONEXCLAMATION | MB_OK);
}


It's kind of messy because of all the debug stuff in there. when i comment the one call of this function out of the program I get no errors. Now what gets me is this: when I debug it line by line, the error is in the D3DSprite->Begin(); function, which if commented will result in errors that aparently occur in my keyboard input functions, and no matter how much I comment out, more errors will spontaniously generate. Now I do have one global pointer but it points to every single thing in the program, and I have a feeling this might be related to the problem but I know nothing of the stack and heap and what not. The problem may also be with the way I'm using arrays in that function (anything clearly wrong there?). Again, with nothing commented out, about 35% of the time the program will run fine until I close it(then errors), the rest of the time immidiatly generating memory read erros. Any ideas or insight would be greatly appreciated, Thanks.

Share this post


Link to post
Share on other sites
Does your device create successfully? Your vertex buffer? What parameters do you pass to create your vertex buffer? What parameters to you pass to the function you showed?

Share this post


Link to post
Share on other sites
heres the vertex buffer initialization function:

HRESULT DISPLAY::InitializeVertexEngine(int maximum_vertices)
{
tester=D3DRenderDevice->CreateVertexBuffer(
maximum_vertices*sizeof(CUSTOM_VERTEX),
0,
D3DFVF_XYZRHW|D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
&D3DVertexBuffer,
NULL);
if(FAILED(tester))
{
MessageBox(window,"Failed to initialize the vertex buffer!","Damn!",MB_ICONEXCLAMATION | MB_OK);
return tester;
}

return NULL;
}






edit: could the fact that I'm calling the loadverticedata() every frame be causing a problem I havn't thought of?

Share this post


Link to post
Share on other sites
No, but it will make it run ungodly slow. Never ever lock a vertex buffer every frame unless it is marked with D3DUSAGE_DYNAMIC.

What is maximum_vertices?

memcpy(pVertices,screen_vertices,sizeof(/*CUSTOM_VERTEX*/screen_vertices)

What is screen_vertices? You have so many variables that there is no way anyone can help without knowing what they are.

[edit] Ignore that. It sounds like you are running out of room on the stack. Instead of D3DXVECTOR2 vertices[1000], try D3DXVECTOR2* vertices = new D3DXVECTOR2[1000]. Of course, remember to do delete[] vertices when the program closes.

Share this post


Link to post
Share on other sites
Do you mean the exact values or what their purpose is? I think the purpose is clear and I dont think the exact value would be of much relavence but maybe I'm wrong.

ps. Never mind that statement I made between time and succesful run correlation, seems it was just a fluke. Also I forgot to mention on rare occasions the program will just close with no read error message at all.

Share this post


Link to post
Share on other sites
0xcdcdcdcd is a hint to the problem. Those values are what the debug runtime fills memory with when it allocates the memory. So the presence of 0xcdcdcdcd suggests you are attempting to use something that has been allocated but not initialized.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just an idea, but you have vertices[1000] and d3dcolor[1000],
don't forget you're only going to have 1 d3dcolor to every three vertices, maybe that isthe problem

Share this post


Link to post
Share on other sites
Well the problem was with how i was determining the number of primitives for every set of primitives.. :(. the catalyst? fatigue. I was just too tired to be thinking straight last night (this morning).

edit: but i still haven't figured out how to get that specific debug window to show up, lol. someone must know.

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