Re: Crash(Access violation) on using virtual function.

Tech-Archive recommends: Fix windows errors by optimizing your registry



manuthomas23@xxxxxxxxx wrote:
Here is the code:
(snip)

/**
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.)


This means you are using uninitialised pointer.
And so "this" is suspected.

You didn't show any constructors of your classes, and how this Engine::Init()
is called, I suspect that you called it from within constructor of Engine,
while "this" or member m_cam is not properly costructed yet.

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.


You mean if you declare local and use like:
M3DFreeCam cam;
cam.Init(this);
It doesn't crash? What is the value of "this"?
And what is the value of "this" when you used as member variable?
How this "this" is used inside M3DFreeCam.Init()? Can you Step Into
the function? or can you pinpoint the exact statement that caused
exception?

Enough question to find answer yourself, I hope...

muchan
.



Relevant Pages

  • Re: Crash(Access violation) on using virtual function.
    ... BOOL res = TRUE; ... Here m_cam is a member variable in Engine. ... DllEntryPoint to initialize your routine. ...
    (microsoft.public.vc.language)
  • Re: Diagnose My Engine Pictures and Win!
    ... rings, you replace cylinders. ... My 75 engine was in similar shape so followed that advice. ... Look at the little line that radiates outward from the valve seat toward the spark plug hole. ... If you get somebody with a valve removing tool to get that valve out for you, I suspect there will be obvious damage. ...
    (rec.autos.makers.vw.aircooled)
  • Re: pointer versus reference in constructors
    ... >>I never really know if I should use references or pointers for constructors. ... >>My back ground is C, so I tend to use pointer notation, but really don't ... > Car myCar(new Engine); ...
    (comp.lang.cpp)
  • Re: The Home-made Home-builders Engine
    ... I suspect that the series of threads have worked their way around ... implied resolution to build one if it can not be purchased new at an unrealistic discount. ... They are probably not especially energy efficient, and the belts are quickly destroyed if the propeller windmills in flight with the engine stopped; but they are a cheap and light weight way to get the job done and are easily inspected. ...
    (rec.aviation.homebuilt)
  • Re: when to change raw water impeller
    ... but I suspect it's roughly the same. ... I suspect the margins are pretty close to the same on all major engine ... These are street prices. ... If the additional weight was pushing the stern down too much, ...
    (rec.boats)