IEventReciver crashes my game loop

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Mloren
Posts: 114
Joined: Mon Aug 07, 2006 2:30 am
Location: Australia
Contact:

IEventReciver crashes my game loop

Post by Mloren »

Hi, just starting out with this engine and following a few tutorials and I have encountered this anoying crash.

If i dont have an EventReciver my game works fin, the moment i pass one to the IrrlichtDevice the game doesnt run.

It crashes at the start of the main game loop.

heres some code:

Code: Select all

int _tmain(int argc, _TCHAR* argv[])
{
	//Create a pointer to the BaseLogic class for easy access.
	BaseLogic* pBaseLogic = BaseLogic::GetInstance();
	//Initialize the BaseLogic class which runs everything else.
	pBaseLogic->Init();
	//Create a pointer to the Irrlicht Engine for easy access.
	IrrlichtDevice* pEngine = pBaseLogic->GetEngine();

	while(pEngine->run())
	{
		if(pEngine->isWindowActive())
		{
			BaseLogic::GetInstance()->Update(0);
		}
		else
			Sleep(100);
	}

	return 0;
}
this is just a modefied event receiver from the terrain tutorial with all the terrain stuff removed.

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:

	MyEventReceiver()
	{
	}

	bool OnEvent(SEvent event)
	{
		if (event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
		{
			switch (event.KeyInput.Key)
			{
			case irr::KEY_KEY_W:
				return true;
			case irr::KEY_KEY_D:
				return true;
			}
		}

		return false;
	}
};

Code: Select all

void BaseLogic::Init()
{

	pEngine = createDevice(EDT_DIRECT3D9,dimension2d<s32>(1024, 768),16,false,false,false,0);
	pEngine->setWindowCaption(L"Bleh");

	MyEventReceiver receiver;

	pEngine->setEventReceiver(&receiver);
	
	pVideoDriver = pEngine->getVideoDriver();
	pScene = pEngine->getSceneManager();
	
	Camera::GetInstance()->Init();	//Initialise the Camera
	Gui::GetInstance()->Init();		//Initialize the Gui

	
	//TEMP//
	IAnimatedMesh* mesh = pScene->getMesh("./sydney.md2");
	IAnimatedMeshSceneNode* node = pScene->addAnimatedMeshSceneNode( mesh );

	if (node)
	{
		node->setMaterialFlag(EMF_LIGHTING, false);
		node->setFrameLoop(0, 310);	
		node->setMaterialTexture( 0, pVideoDriver->getTexture("./sydney.bmp") );
	}

	//pScene->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
	//END TEMP//
}
if i comment out the pEngine->setEventReceiver(&receiver); then it runs fine, but with that code it crashes in the main() on while(pEngine->run())

the error is
Unhandled exception at 0x06a4f99c in Cislunar.exe: 0xC0000005: Access violation reading location 0x06a4f99c.

I have tried lots of different bits of EventReceiver code, from different tutorials, none work for me.

oh btw im using Visual C++ 2005 Express Edition if that makes any difference.
Mikenoworth
Posts: 78
Joined: Sat May 27, 2006 9:24 pm
Location: Logan, UT

Post by Mikenoworth »

Receiver isn't persistant.

Code: Select all

MyEventReceiver receiver; 
At the end of the function Init(), receiver no longer exists, setEventReceiver() does not create a copy of the class from the pointer, so when the callback tries to call the function it calls an invalid address and causes a crash.

You could make it a pointer to the class, and use new to alloc mem for it to make it persistant.

Code: Select all

MyEventReceiver* receiver = new MyEventReceiver(); 
then use delete to free the mem in the destructor.

Or you could simply put the receiver as a class member in the class header. Like so

Code: Select all

// someclass.h

class someclasss
{
    // constructors, destructors blah blah..
    // ...

    protected:
           MyEventReceiver receiver;
};
Either way, they both work.
Stout Beer
Mloren
Posts: 114
Joined: Mon Aug 07, 2006 2:30 am
Location: Australia
Contact:

Post by Mloren »

Yep that fixed it.
hehe I cant believe i didnt think of that.
thanks :)
Mikenoworth
Posts: 78
Joined: Sat May 27, 2006 9:24 pm
Location: Logan, UT

Post by Mikenoworth »

No problem. :D
Stout Beer
wooohoh
Posts: 5
Joined: Fri Aug 25, 2006 9:02 am

Post by wooohoh »

wow that helps! i was struggling with exactly same problem :D
Post Reply