Jump to content
  • Advertisement
Sign in to follow this  

cant pass DX pointer to class

This topic is 3999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this class function will compile but i get access violation. If i have this function NOT in a class it works fine. The error at runtime has the pointer g_mesh as being empty. The pointer is not being re-assigned in my class function why wont this work in a class but will out of a class?

it gets called from a main program
	if (FAILED(Scene_object.initVB2(g_pd3dDevice, g_mesh2)))
		MessageBox(0, L"initVB", 0, MB_OK);

in my main file i use it again assuming the pointer gets changed without returning it.
	for(int i = 0; i <NumSubsets; i++)
			g_pd3dDevice->SetTexture( 0, g_tex );
			g_mesh2->DrawSubset( i );//ACCESS VIOLATION ERROR

and in the class i define

HRESULT scene_object::initVB2( LPDIRECT3DDEVICE9 g_pd3dDevice,  LPD3DXMESH g_mesh2)
struct Vertex2
	Vertex2(float x, float y, float z, 
		float nx, float ny, float nz, float u, float v)
		 _x = x;   _y = y;   _z = z;
		_nx = nx; _ny = ny; _nz = nz;
		 _u = u;   _v = v;

	float _x, _y, _z, _nx, _ny, _nz, _u, _v;

	//static const DWORD FVF;

		D3DFVF_CUSTOMVERTEX,g_pd3dDevice ,&g_mesh2)))
		MessageBox(0, L"D3DXCreateMeshFVF() - FAILED", 0, 0);
		return false;
		// Fill in vertices of a box
	Vertex2 *v2 = 0;
	g_mesh2->LockVertexBuffer(0, (void**)&v2);

	// fill in the bottom face vertex data

	v2[0] = Vertex2(-100.0f, -1.0f, -100.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f);
	v2[1] = Vertex2( 100.0f, -1.0f, -100.0f, 0.0f, -1.0f, 0.0f, 0.0f, 100.0f);
	v2[2] = Vertex2( 100.0f, -1.0f,  100.0f, 0.0f, -1.0f, 0.0f, 100.0f, 100.0f);
	v2[3] = Vertex2(-100.0f, -1.0f,  100.0f, 0.0f, -1.0f, 0.0f, 100.0f, 0.0f);


	// Define the triangles of the box
	WORD *i2 = 0;
	g_mesh2->LockIndexBuffer(0, (void**)&i2);

// fill in the bottom face index data
	i2[0] = 0; i2[1] =  1; i2[2] = 2;
	i2[3] = 0; i2[4] = 2; i2[5] = 3;

		DWORD* attributeBuffer2 = 0;
	g_mesh2->LockAttributeBuffer(0, &attributeBuffer2);

	for(int a = 0; a < 2; a++)
		attributeBuffer2[a] = 0;


	return S_OK;

[Edited by - jagguy2 on January 3, 2008 12:05:54 AM]

Share this post

Link to post
Share on other sites
I imagine this has little to do with existence of classes and more to do with order of initialisation.

Put a breakpoint on the code that initialises the IDirect3DDevice9 object pointer, and another on the class member function that causes the access violation. If the object is being used before it is created then you obviously have a design problem.

You haven't shown us the scope at which the relevant variables are declared, but be aware that the constructor of a globally-defined class instance will execute before the program's main function. And it may just be your nomenclature, but g_pd3dDevice looks like a global variable. If it is then there's probably no need to pass it as a function argument. Use one or the other, preferably the latter as it tends to help avoid problems just like the one you're experiencing.

If you're still a little lost with regards to these design principles then I suggest you read up on RAII: it can make your life a lot easier when dealing with resource management.

Share this post

Link to post
Share on other sites
Change this line:

HRESULT scene_object::initVB2( LPDIRECT3DDEVICE9 g_pd3dDevice,
LPD3DXMESH g_mesh2);

to this:

HRESULT scene_object::initVB2( LPDIRECT3DDEVICE9 g_pd3dDevice,
LPD3DXMESH &g_mesh2); // Note the '&'

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!