Program crashes on calling virtual function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • manuthomas23@gmail.com

    #1

    Program crashes on calling virtual function

    Here is the code:

    #ifndef M3DCAMERA__H__
    #define M3DCAMERA__H__

    class M3DEngine;

    /**
    Micro3D Camera class.
    */
    class M3DCam
    {
    public:
    virtual void Init(EngineData *ed);
  • Ben Pope

    #2
    Re: Program crashes on calling virtual function

    manuthomas23@gm ail.com wrote:[color=blue]
    > Here is the code:[/color]

    What code?
    [color=blue]
    > #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.
    > };[/color]

    What is EngineData?
    What is an IMicro3D?
    What is a Vec3i?
    What is an Atrans3i?
    My compiler isn't fond of the dots, either.
    [color=blue]
    > /**
    > 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.
    > };[/color]

    What is hi_sint32?
    [color=blue]
    > /**
    > Base Engine class.
    > */
    > class Engine : public EngineData
    > {
    > private:
    >
    > M3DFreeCam m_cam;
    > .
    > .
    > .
    > }
    >
    > /**
    > Initialize the game engine.
    > \return TRUE if success.
    > */
    > BOOL Engine::Init(vo id)
    > {
    > BOOL res = TRUE;
    >
    > EngineData::Ini t();
    >
    > SetGameState((G SFunc)Engine::G S_Game, GS_GAME);[/color]

    <<-- CAST ABOVE

    What is GSFunc?
    [color=blue]
    > m_cam.Init(this ); <<-----------CRASHES HERE[/color]



    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...

    Comment

    • Fei Liu

      #3
      Re: Program crashes on calling virtual function


      manuthomas23@gm ail.com wrote:[color=blue]
      > 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(vo id)
      > {
      > BOOL res = TRUE;
      >
      > EngineData::Ini t();
      >
      > SetGameState((G SFunc)Engine::G S_Game, GS_GAME);
      >
      > m_cam.Init(this ); <<-----------CRASHES HERE
      > m_cam.MoveForwa rd(10);
      > m_m3dengine.Sel ectCamera(&m_ca m);
      >
      > xit:
      > return res;
      > }
      >
      > Here m_cam is a member variable in Engine. It is of type
      > M3DFreeCam(deri ved 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.e xe:
      > 0xC0000005: Access violation reading location 0x00000000.)
      >
      > If I make m_cam a local variable(instea d 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.[/color]

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

      Comment

      Working...