Why does my app crash

Started by
7 comments, last by KingsRevenge 21 years, 8 months ago
My code is failing inside of this function in the first DrawPrimitive call from what debugger tells me and waht my own personal debugging has been through.
  

void CTerrain::Draw()
{
	
	g_pDevice->SetStreamSource(0,pTerrainVertexBuffer,sizeof(D3DVERTEX));
	
	g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,16129);

	g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,48387,16129);
	
}
  
I have no idea why it''s crashing though I kind of believe it could be dealing with my vertexbuffer code which is dealt with in this section.
   
void CTerrain::CreateVertexBuffer(void)
{
	static D3DVERTEX aTerrain[(TERRAIN_X - 1) * (TERRAIN_Y - 1) * 6];
	unsigned char aHeightMap[TERRAIN_X][TERRAIN_Y];
	int VertexCount = 0;
	ifstream File;
	BYTE *pData;

	File.open("heightmap.raw",ios::binary);

	for(int j = 0; j < TERRAIN_Y; j++)
	{
		for(int i = 0; i < TERRAIN_X; i++)
		{
			File.get(aHeightMap[i][j]);
		}
	}

	File.close();

	for(int y = 0; y < TERRAIN_Y - 1; y++)
	{
		for(int x = 0; x < TERRAIN_X - 1; x++)
		{
			aTerrain[VertexCount].fX = (float)x;
			aTerrain[VertexCount].fY = (float)y;
			aTerrain[VertexCount].fZ = (float)aHeightMap[x][y] / 10;

			aTerrain[VertexCount+ 1].fX = (float)x+1;
			aTerrain[VertexCount+ 1].fY = (float)y;
			aTerrain[VertexCount+ 1].fZ = (float)aHeightMap[x+1][y] / 10;

			aTerrain[VertexCount+ 2].fX = (float)x+1;
			aTerrain[VertexCount+ 2].fY = (float)y+1;
			aTerrain[VertexCount+ 2].fZ = (float)aHeightMap[x+1][y+1] / 10;
		
			aTerrain[VertexCount + 3].fX = (float)x;
			aTerrain[VertexCount + 3].fY = (float)y;
			aTerrain[VertexCount + 3].fZ = (float)aHeightMap[x][y] / 10;
			
			aTerrain[VertexCount + 4].fX = (float)x + 1;
			aTerrain[VertexCount + 4].fZ = (float)aHeightMap[x + 1][y + 1] / 10;
			aTerrain[VertexCount + 4].fY = (float)y + 1;

			aTerrain[VertexCount + 5].fX = (float)x;
			aTerrain[VertexCount + 5].fY = (float)y + 1;
			aTerrain[VertexCount + 5].fZ = (float)aHeightMap[x][y + 1] / 10;
			
			VertexCount +=6;
		}
	}
	
	g_pDevice->CreateVertexBuffer(sizeof(aTerrain)*VertexCount,NULL,
								  D3DFVF_CUSTOMVERTEX,
								  D3DPOOL_DEFAULT,
								  &pTerrainVertexBuffer);
	
	if(FAILED(pTerrainVertexBuffer->Lock(0,sizeof(pData),(BYTE **)&pData,0)))
	{
		MessageBox(NULL,"Locked","Locked",MB_OK);
	}
	CopyMemory(pData,&aTerrain,sizeof(aTerrain));
	pTerrainVertexBuffer->Unlock();
}

  
could any of you provide me some help to why my app isnt displaying and running when i have those 2 draw primitive calls runnin, when i comment them out the app runs fine?? Eric Wright o0Programmer0o
Eric Wright o0Programmer0o
Advertisement
How could you make a so basic mistake, I think it due to a lack of cofee :D

You do this to lock the vertex buffer
if(FAILED(pTerrainVertexBuffer->Lock(0,sizeof(pData),(BYTE **)&pData,0)))


But it lock only 4 byte, because pData is a pointer you can''t use sizeof (you can on constant array).

You must do:

// Since the array is a D3DVERTEX, the sizeof will return the corret size in byte (don''t multiply by VertexCount)
g_pDevice->CreateVertexBuffer(sizeof(aTerrain),NULL, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &pTerrainVertexBuffer);

if(FAILED(pTerrainVertexBuffer->Lock(0,sizeof(aTerrain),(BYTE **)&pData,0)))



Good Luck
I made the chagnes you stated and the app still crashes . Any other ideas?
Eric Wright o0Programmer0o
nice to meet my most helpful writer except for the founder of Nexe.
I don''t know how work your CopyMemory function but it''s strange to pass "&aTerrain" (this is a unsigned char**) is it intend ?

Is your D3DFVF_CUSTOMVERTEX define like this:
#define D3DFVF_CUSTOMVERTEX D3DFVF_XYZ

If not, this may be the cause of app crash

Have you any other things drawn ?

If it still crash, you can send me you code via email.
It''s a little hard to know what gone wrong only with this code..

Good Luck :D
CopyMemory is an Windows API function (unless it''s not the same CopyMemory here). Either way, taking the address of aTerrain won''t yield the address of something "useful," and I wouldn''t be a bit surprised if that were the/a problem right there. All you need is to pass aTerrain because that expression yields the address of the array, which is exactly what you want.
I used CopyMemory and my program crashed, I replaced it with memcpy and it worked fine. Try it...
No matter where you go... &this
quote:Original post by Viquel Lukhar
I don''t know how work your CopyMemory function but it''s strange to pass "&aTerrain" (this is a unsigned char**) is it intend ?

Because aTerrain is a statically allocated array the following is true:

(aTerrain == &aTerrain)

In other words: &aTerrain is not a char** (aTerrain isn''t even a char*).

Hmm. I just tried it out and you''re exactly right, both about the equivalence and the stipulation that it only works with a stack-allocated object. Anyway, if you ask me, it''s safest not to take the address of an array since you won''t get a "useful" address if the array is dynamically-allocated.

This topic is closed to new replies.

Advertisement