Re: Crash(Access violation) on using virtual function.
- From: muchan <muchan@xxxxxxxxxxx>
- Date: Tue, 07 Mar 2006 13:38:08 +0100
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
.
- Follow-Ups:
- References:
- Crash(Access violation) on using virtual function.
- From: manuthomas23
- Crash(Access violation) on using virtual function.
- Prev by Date: Days in month function
- Next by Date: Re: How to Open a File In Binary Mode using fopen
- Previous by thread: Crash(Access violation) on using virtual function.
- Next by thread: Re: Crash(Access violation) on using virtual function.
- Index(es):
Relevant Pages
|