Page 1 of 1

EventReceiver causes crash [Solved]

Posted: Tue May 24, 2005 3:38 pm
by Saku
Hi,

As the title says my EventReceiver crashes my game. The game runs perfectly when I comment out:
" device->setEventReceiver(&receiver); "

My EventReceiver is placed in some cpp and h files besides the core.
The weird part is my game does not crash here but first when I use this line:
" ISceneNode* map = smgr->addOctTreeSceneNode(smgr->getMesh(file)); "

I even declare a couple of pointers in some lines between.

Anyone else have had the same problem or maybe just have a solution?? :?:

/Saku

Posted: Wed May 25, 2005 8:02 am
by Thulsa Doom
Your EventReceiver may implement an OnEvent() function but does not inherit from IEventReceiver(). After a mesh is successfully loaded, a message is passed to the EventReceiver and the exeption occurs.

Posted: Wed May 25, 2005 8:43 am
by Saku
I'm not too sure but I think it does..

CEventReceiver.h

Code: Select all

#ifndef CEVENTRECEIVER_H
#define CEVENTRECEIVER_H

#include "irrHead.h"

class MyEventReceiver : public IEventReceiver
{     
public:
	virtual bool OnEvent(SEvent event);
	void manageEvents();
};

#endif
Did I get it right?

Posted: Wed May 25, 2005 1:30 pm
by Thulsa Doom
The exception seems to occur ether in

virtual bool OnEvent(SEvent event);
or
void manageEvents();

try to backtrace it.

Posted: Wed May 25, 2005 3:29 pm
by Saku
Backtrace how? :?
I've tried to comment out most but it still crashes. Remaining code:

CEventReceiver.cpp :

Code: Select all

#include "irrHead.h"
#include "CEventReceiver.h"

extern IrrlichtDevice* device;

bool MyEventReceiver::OnEvent(SEvent event)
{
	return true;
}
CEventReceiver.h :

Code: Select all

#ifndef CEVENTRECEIVER_H
#define CEVENTRECEIVER_H
#include "irrHead.h"

class MyEventReceiver : public IEventReceiver
{     
public:
	virtual bool OnEvent(SEvent event);
};
#endif 
Thanks so far :!: :D

Posted: Wed May 25, 2005 4:45 pm
by bal
I don't think this really matters but now you're constantly sending events, so ry changing "return true;" into "return false;". How do you declare the variable "receiver" ? Make sure it's declared as "MyEventReceiver receiver;" and not as a pointer, although, that should throw a compile-time error...

Posted: Wed May 25, 2005 6:37 pm
by Saku
In my case it doesn't matter. I've tried both and none works.
I double checked the declaration and I do exactly like you suggest.

Posted: Fri May 27, 2005 6:37 am
by Guest

Code: Select all

class MyEventReceiver : public IEventReceiver
{     
public:
   virtual bool OnEvent(SEvent event);
   void manageEvents();
}; 
Drop the virtual keyword and/or add a constructor.
If it does'nt work, I'm running out of ideas.

T.D. :?

Posted: Fri May 27, 2005 11:17 am
by Guest
Neither of those two should make a difference - you don't need a constructor if you're not defining any extra fields, and virtual just means that the function can be overridden in derived classes.

But I'm surprised at how little information Saku is giving. Frankly I'm surprised bal and TD have been able to give as much advice as they did. Am I not seeing a link or something?

Anyway, post the relevant section of your main function. We should at least be able to look at any event receiver related stuff there - since the most likely cause of a crash is an invalid pointer.

Posted: Fri May 27, 2005 12:09 pm
by Saku
You are so very right! None of them worked.
And if you want more of the code, just ask. I have no problem posting all my code here, but I thought ppl would just be annoyed by page on page of unrelevant code. I've packed all my code and uploaded here: http://www.zOneOne.dk/files/pack.zip

Also I'm very thankfull for ppl being willing to help me.
If you got any questions or anything just ask!

/Saku

Posted: Fri May 27, 2005 3:04 pm
by ondrew
easy stuff, the same happened to me.

you are declaring your variable "receiver" in CCore.cpp in InitDevice(), but it's only a local variable. So as soon as InitDevice() ends, your variable "receiver" is destroyed, memory freed ...

I would define your receiver as part of the CCore class.

Code: Select all

class CCore
{
public:
        void InitDevice();
        void InitGame();                                                                
        void Run();
        MyEventReceiver receiver;
};

Posted: Fri May 27, 2005 4:58 pm
by Saku
:shock: ..it worked..
:oops: Did I mention I'm a noob ? :lol:

THANKS! Thank you Ondrew!
And thank you all for being pacient with me 8)

/Saku