Uh-oh... Access error

Started by
2 comments, last by Xtremehobo 20 years, 9 months ago
i''ve got this code:

HRESULT InitTerrain()
{
    // Initialize four vertices for rendering a square {  -1.0f, -1.0f, 0.0f, 0xffffffff, 0, 1 }, // Bottom Left

    LoadHeightmap();
	CUSTOMVERTEX m_Vertices[40000];
	char buffer[255];
	
    for(int x=0;x<200;x++)
	{
		for(int z=0;z<200;z++)
		{
			m_Vertices[(unsigned)(x*200+z)].x     = x;
			m_Vertices[(unsigned)(x*200+z)].y     = Heightmap[x][z];
			m_Vertices[(unsigned)(x*200+z)].z     = z;
			m_Vertices[(unsigned)(x*200+z)].color = 0xffffffff;
			m_Vertices[(unsigned)(x*200+z)].tu    = 1;
			m_Vertices[(unsigned)(x*200+z)].tv    = 1;
			sprintf(buffer,"%i\n",Heightmap[x][z]);
			OutputDebugString(buffer);
			
		}
	}

	
	// Initialize six indicesfor redering a square

	short g_Indices[] = { 0, 1, 3, 3, 1, 2 };

    // Create the vertex buffer.

    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 40000*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pTerrainVB, NULL ) ) )
    {
        return E_FAIL;
    }
.....
It gives me an access violation when CreateVertexBuffer, but when I uncomment the first line of that function (LoadHeightmap() I don''t get the access error and everything runs fine. Incase you need to know, here''s the source for LoadHeightmap

VOID LoadHeightmap()
{
//	MessageBox(NULL,"Loading Heightmap","D3D",MB_OK);

	//Load the heightmap

	HANDLE HeightmapImage = LoadImage(0,"C:\\heightmap.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	HDC    HeightmapDC = CreateCompatibleDC(GetDC(GetActiveWindow()));
	SelectObject(HeightmapDC,HeightmapImage);

	for(int x=0;x<201;x++)
	{
		for(int y=0;y<201;y++)
		{
			//Add values into Heightmap array

			Heightmap[x][y] = GetRValue(GetPixel(HeightmapDC,x,y));
		}
	}
}
any help would be greatly appriciated. Thanks
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
Advertisement
Guesses:

1. Something is overwriting your D3D device pointer - when you get the access violation in the debugger, if it stops on the CreateDevice() line in the source code rather than inside the CreateDevice code itself, check the address of all the pointers.
If "Heightmap" wasn''t allocated properly, or didn''t have space for 40401 entries (201x201), I''d say that''s the culprit for a memory trash.

2. Remember to get rid of the DC and the loaded image properly when you''ve finished with them otherwise over time your code will suck up more and more memory each time you run the program.

--
Simon O''Connor
ex -Creative Asylum
Programmer &
Microsoft MVP

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Looks like in my LoadHeightmap code, Heightmap wasn''t allocated with enough space
The for loops should have been "x<200" and "y<200" not 201
Thanks
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
I would probably add in some error checking to make sure HeightmapImage and HeightmapDC are valid.

for example
if(HeightmapImage==NULL)
// Your stuffed.

Could be a simple problem like not loading the image.

This topic is closed to new replies.

Advertisement