Program crashes on calling virtual function

M

manuthomas23

Here is the code:

#ifndef M3DCAMERA__H__
#define M3DCAMERA__H__

class M3DEngine;

/**
Micro3D Camera class.
*/
class M3DCam
{
public:
virtual void Init(EngineData *ed);
.
.
.
protected:
IMicro3D *m_pmicro3d; ///<Micro3D interface pointer.
Vec3i m_pos; ///<Position of the camera.
Vec3i m_target; ///<Target of the camera.
Vec3i m_up; ///<Camera up direction vector.
Atrans3i m_cam_mat; ///<Camera matrix.
};

/**
Free moving Camera.
*/
class M3DFreeCam : public M3DCam
{
public:
void Init(EngineData *ed);
void CleanUp(void);

.
.
.
.
private:
hi_sint32 m_cam_ang; ///<Angle of rotation of camera w.r.t Y axis.
Vec3i m_dir; ///<Direction vector of movement.
};

/**
Base Engine class.
*/
class Engine : public EngineData
{
private:

M3DFreeCam m_cam;
.
.
.
}

/**
Initialize the game engine.
\return TRUE if success.
*/
BOOL Engine::Init(void)
{
BOOL res = TRUE;

EngineData::Init();

SetGameState((GSFunc)Engine::GS_Game, GS_GAME);

m_cam.Init(this); <<-----------CRASHES HERE
m_cam.MoveForward(10);
m_m3dengine.SelectCamera(&m_cam);

xit:
return res;
}

Here m_cam is a member variable in Engine. It is of type
M3DFreeCam(derived from MD3Cam). Init() is a virtual function. But when
I call m_cam.Init(this), the program crashes!

(Unhandled exception at 0x00f09f33 (pool.dll) in BREW_Emulator.exe:
0xC0000005: Access violation reading location 0x00000000.)

If I make m_cam a local variable(instead of member variable),
inside Engine::Init(), it works fine, no crashes. I wonder why this is
happening. Could anyone please explain this to me?

Thanks in advance.
 
B

Ben Pope

Here is the code:

What code?
#ifndef M3DCAMERA__H__
#define M3DCAMERA__H__

class M3DEngine;

/**
Micro3D Camera class.
*/
class M3DCam
{
public:
virtual void Init(EngineData *ed);
.
.
.
protected:
IMicro3D *m_pmicro3d; ///<Micro3D interface pointer.
Vec3i m_pos; ///<Position of the camera.
Vec3i m_target; ///<Target of the camera.
Vec3i m_up; ///<Camera up direction vector.
Atrans3i m_cam_mat; ///<Camera matrix.
};

What is EngineData?
What is an IMicro3D?
What is a Vec3i?
What is an Atrans3i?
My compiler isn't fond of the dots, either.
/**
Free moving Camera.
*/
class M3DFreeCam : public M3DCam
{
public:
void Init(EngineData *ed);
void CleanUp(void);

.
.
.
.
private:
hi_sint32 m_cam_ang; ///<Angle of rotation of camera w.r.t Y axis.
Vec3i m_dir; ///<Direction vector of movement.
};

What is hi_sint32?
/**
Base Engine class.
*/
class Engine : public EngineData
{
private:

M3DFreeCam m_cam;
.
.
.
}

/**
Initialize the game engine.
\return TRUE if success.
*/
BOOL Engine::Init(void)
{
BOOL res = TRUE;

EngineData::Init();

SetGameState((GSFunc)Engine::GS_Game, GS_GAME);

<<-- CAST ABOVE

What is GSFunc?
m_cam.Init(this); <<-----------CRASHES HERE



Remove the cast. Fix the problem properly.

If you want us to help, post complete, compilable code. Don't add
extraneous dots, as they don't compile.

Ben Pope
 
F

Fei Liu

Here is the code:

#ifndef M3DCAMERA__H__
#define M3DCAMERA__H__

class M3DEngine;

/**
Micro3D Camera class.
*/
class M3DCam
{
public:
virtual void Init(EngineData *ed);
.
.
.
protected:
IMicro3D *m_pmicro3d; ///<Micro3D interface pointer.
Vec3i m_pos; ///<Position of the camera.
Vec3i m_target; ///<Target of the camera.
Vec3i m_up; ///<Camera up direction vector.
Atrans3i m_cam_mat; ///<Camera matrix.
};

/**
Free moving Camera.
*/
class M3DFreeCam : public M3DCam
{
public:
void Init(EngineData *ed);
void CleanUp(void);

.
.
.
.
private:
hi_sint32 m_cam_ang; ///<Angle of rotation of camera w.r.t Y axis.
Vec3i m_dir; ///<Direction vector of movement.
};

/**
Base Engine class.
*/
class Engine : public EngineData
{
private:

M3DFreeCam m_cam;
.
.
.
}

/**
Initialize the game engine.
\return TRUE if success.
*/
BOOL Engine::Init(void)
{
BOOL res = TRUE;

EngineData::Init();

SetGameState((GSFunc)Engine::GS_Game, GS_GAME);

m_cam.Init(this); <<-----------CRASHES HERE
m_cam.MoveForward(10);
m_m3dengine.SelectCamera(&m_cam);

xit:
return res;
}

Here m_cam is a member variable in Engine. It is of type
M3DFreeCam(derived from MD3Cam). Init() is a virtual function. But when
I call m_cam.Init(this), the program crashes!

(Unhandled exception at 0x00f09f33 (pool.dll) in BREW_Emulator.exe:
0xC0000005: Access violation reading location 0x00000000.)

If I make m_cam a local variable(instead of member variable),
inside Engine::Init(), it works fine, no crashes. I wonder why this is
happening. Could anyone please explain this to me?

Thanks in advance.

It seems something is messed in the initialization order. It's not
clear what the problem is from the code snippets you have posted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top