getTexture calls OnEvent

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
gheft
Posts: 34
Joined: Mon Jul 30, 2007 4:11 am

getTexture calls OnEvent

Post 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
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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!
Image Image Image
gheft
Posts: 34
Joined: Mon Jul 30, 2007 4:11 am

Post 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 :)
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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 ;)
Image Image Image
gheft
Posts: 34
Joined: Mon Jul 30, 2007 4:11 am

Post 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 :?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
Post Reply