By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

Program crashes on calling virtual function

P: n/a
Here is the code:

#ifndef M3DCAMERA__H__
#define M3DCAMERA__H__

class M3DEngine;

/**
Micro3D Camera class.
*/
class M3DCam
{
public:
virtual void Init(EngineData *ed);
Mar 7 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ma**********@gmail.com wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Mar 7 '06 #2

P: n/a

ma**********@gmail.com wrote:
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.

Mar 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.