Advertisement

Class Access Violation

Started by August 04, 2002 05:19 PM
3 comments, last by JimboC 22 years, 3 months ago
He's back and stuck again! CGame is a child class of base class CBase. CGame creates a private member of type CLaserTurret which is also a child of CBase. When I render the private member m_pLaserTurret, I get an Access Violation. Function that crashes:
    
HRESULT CGame::Render ( )
{
    if ( !g_pd3dDevice )
		{
			SetError ( "Unable to render because there's no device." );
			return E_FAIL;
		}
	
	if ( FAILED ( m_ValidateDevice ( ) ) )
		return E_FAIL;		

	g_pd3dDevice->Clear ( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
		                 D3DCOLOR_XRGB(0,0,64), 1.0f, 0 );
 	
	// Begin the scene

	g_pd3dDevice->BeginScene ( );
	m_SetupWorldMatrices ( );
	
        ////////////////////////////////////

	m_pLaserTurret->m_Render ( ); // <-- This line crashes program

        ////////////////////////////////////

	
	g_pd3dDevice->EndScene ( );

	// Present the backbuffer contents to the display

	g_pd3dDevice->Present( NULL, NULL, NULL, NULL );

	return S_OK;
}  
  
Crashes on the if statement:
          
//--------------------------------------------------------------------

// CLaserTurret::m_Render ( )

//--------------------------------------------------------------------

void CLaserTurret::m_Render ( )
{
	if(	m_pMesh != NULL )
	{
		for( DWORD i = 0; i < m_dwNumMaterials; i++)
		{
			g_pd3dDevice->SetMaterial( &m_pMeshMaterials[i]);
			g_pd3dDevice->SetTexture(0, m_pMeshTextures[i]);
        
			m_pMesh->DrawSubset(i);
		}
	}
	return;
}
  
I've tried moving everything in the CLaserTurret class to Public, but it doesn't help. I've also tried making CGame a friend of CLaserTurret. Here's the definition of both classes: CGame
    
#include "CBase.h"

class CLaserTurret;

#ifndef CGAME_HEADER
#define CGAME_HEADER
//------------------------------------------------

// Class CGame

//------------------------------------------------

class CGame : public CBase
{
public:
    CGame ( );
	virtual ~CGame ( );
	void m_GameLoop ( );
	HRESULT Render ( );
	void m_SetupWorldMatrices ( );
	HRESULT m_ValidateDevice ( );

private:
	CLaserTurret* m_pLaserTurret;

};

#endif
  
CLaserTurret
  
#include "CBase.h"

#ifndef CLASERTURRET_HEADER
#define CLASERTURRET_HEADER
//------------------------------------------------

// Class m_CLaserTurret

//------------------------------------------------

class CLaserTurret : public CBase
{
public:
	friend class CGame;
	CLaserTurret ( );
	virtual ~CLaserTurret ( );
	void m_Render ( );

private:
	DWORD m_dwNumMaterials;
	LPD3DXMESH m_pMesh;
	D3DMATERIAL8* m_pMeshMaterials;
	LPDIRECT3DTEXTURE8* m_pMeshTextures;

};

#endif
  
Any information on what's wrong would be greatly appreciated! [edited by - JimboC on August 4, 2002 6:23:27 PM]
Jimbo:

I'm a relative newbie myself, but this sort of popped out at me:

You're trying to do this:

----
m_pLaserTurret->m_Render ( ); // <-- This line crashes program
----

When you've declared this:

----
CLaserTurret* m_pLaserTurret;
----

What is *m_pLaserTurret pointing to? From the infromation you've put in, and the error message, I would surmise that you've declared the pointer, but it's not pointing to anything. Of course, I could be totally wrong, too.

John.



[edited by - JohnAD on August 5, 2002 6:36:32 PM]
Advertisement
Well, that wasn''t it exactly, but it got me thinking. I found that by moving the m_pLaserTurret out of the CGame constructor and into a separate method it didn''t get filled with garbage and the program stopped crashing.

Thanks a bunch!
No Problem !

John.
Is the CGame that you are rendering a global variable? Cause if that is the case, I can explain the reason for the problems (this may apply if it is not a global, but it doesn''t have to)

The constructor gets called the instant that the class instance is declared. So if your declaration is before DirectX (or whatever library you use) is initialized, the constructor may not work properly.

Hopefully this will explain the reason, since it is always good to know why something happens not just what happens.

Feel free to email me at NYYanks432@hotmail.com if you have any questions
Feel free to email me at NYYanks432@hotmail.com if you have any questions

This topic is closed to new replies.

Advertisement