# 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;
{
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 on other sites
the error I'm getting pretty consistantly now is "the instruction at "blah" referenced by memory "0xcdcdcdcd". the memory could not be read."

##### 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 on other sites
Nothing is failing as far as I know.I pass two arrays declared as follows:
D3DXVECTOR2 vertices[1000];
and D3DCOLOR colors[1000];

in the function that calls loadvertices().

##### 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 on other sites
There is a direct correlation to the time I wait befor running and the probability that it will run and it seems almost exclusively dependent on that correlation.

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

 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 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 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 on other sites
god this sucks, how do i get the little window that shows you what variable has what value (and automatically put varibles in there, has the 'local' and 'auto' tab)when your debugging in VC++ 6

##### Share on other sites
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 on other sites
Hm, thank you for the input but that's simply not the case :), if you give each vertice in a polygon a diferent color, it well blend all three,(at least in the version I'm usnig).

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628342
• Total Posts
2982177

• 9
• 24
• 9
• 9
• 13