Page 1 of 1

getTexture calls OnEvent

Posted: Wed Jul 30, 2008 11:12 pm
by gheft
I don't know if this is a bug or if there is some strange reason why this behaviour would be expected, but it seems that calling driver->getTexture() also calls my event receivers onEvent().
It gave me an access violation because my onEvent() references variables that have not yet been initialized.

I'm pretty sure this is what is happening because when I set a breakpoint right after getTexture it crashes before it gets there with an access violation in onEvent, and the callstack points right to where driver->getTexture() is called.

I'm using irrlicht 1.4.1 and visual studio 2008

Posted: Wed Jul 30, 2008 11:41 pm
by hybrid
The event receiver is also called for log messages, which are printed very early. You might be better off with passing only working event receivers to the device.

Posted: Thu Jul 31, 2008 7:45 am
by JP
You should never access pointers aren't valid. You should always check that a pointer is valid before use so if you've got a node that your event receiver might use then you should always have if(node) before you do anything with it.

That's just standard programming practice!

Posted: Fri Aug 01, 2008 6:24 am
by gheft
ah right log messages, you're both right actually but I'm just gonna take the easy way out and go

if(event.EventType == EET_LOG_TEXT_EVENT)
{
return true;
}
thanks guys :)

Posted: Fri Aug 01, 2008 7:07 am
by JP
So you're just gonna continue not checking pointers and hoping for the best that they're valid? That's gonna give you a lot of headaches later (just like it gave you a seemingly random crash with this problem), but hey, if that's the way you wanna work then that's the way you'll work ;)

Posted: Fri Aug 01, 2008 9:43 am
by gheft
when you say check a pointer do you mean checking a pointer and reporting an error if it isn't valid?
I'd rather not check every pointer everytime I want to use it, surely it's better to make sure it is only accessed after it is initialized and having a better understanding of the programs flow.

if I have an invalid pointer I think I would have to debug something anyway and skipping a chunk of code doesn't help.
plus its easier to read :?

Posted: Fri Aug 01, 2008 3:06 pm
by vitek
You don't need to display a message, you just need to avoid using the pointer that isn't valid.

It sounds like your code is something like this...

Code: Select all

// the global Irrlicht device (bad idea to begin with)
IrrlichtDevice* Device = 0;

class MyEventReceiver : public IEventReceiver
{
public:
  virtual bool OnEvent (const SEvent& event)
  {
    Device->getTimer ();
  }
};

int main ()
{
  MyEventReceiver receiver;

  Device = createDevice (video::EDT_SOFTWARE, core::dimension2di(640, 480), 16, false, false, false, &receiver);

  Device->drop ();
  Device = 0;
}
If the Device pointer isn't checked inside of the event receiver, it will still be NULL when MyEventReceiver::OnEvent() is called. That pointer won't be valid until after the call to createDevice() has completed. One way around this is to check the pointer before you use it. Another way is to avoid registering your event receiver until after the system is fully initialized.

BTW, if you're not checking pointers because it makes your code easier to read, you're just asking for trouble.

Travis