Page 1 of 1

Event Reciever occasionally crashes (rarely)

Posted: Thu Mar 04, 2004 5:28 am
by thesmileman
Hey guys my program occasionally crashes at startup or when I am moving around my FPS camera fast. The debugger says the problem is in
(irrDevice != 0 && irrDevice->getSceneManager()->getActiveCamera())

I put in the "irrDevice != 0" becuase after I started using version 0.5 the program kept crashing because irrDevice was not initialised(though I do not know why the code would have worked in 4.0 if that was the case)

If you can see anything I am doing wrong let me know. Thanks!

Code: Select all

class MyEventReceiver : public IEventReceiver {
   public:
      virtual bool OnEvent(SEvent event) {
         //Store Key input for control of objects and actions
         keys[event.KeyInput.Key] = event.KeyInput.PressedDown; 
         //Debugger says problem is with line below         
         if(irrDevice != 0 && irrDevice->getSceneManager()->getActiveCamera()) {

return irrDevice->getSceneManager()->getActiveCamera()->OnEvent(event); 
      }
   }
};

where?

Posted: Thu Mar 04, 2004 2:42 pm
by schick
On which line is the SEGFAULT ?

Posted: Thu Mar 04, 2004 5:38 pm
by Gorgon Zola
strange it compiles at all. Not every execution path returns a value!

Posted: Thu Mar 04, 2004 6:41 pm
by thesmileman
The problem is with this line

Code: Select all

if(irrDevice != 0 && irrDevice->getSceneManager()->getActiveCamera())
And Gorgon Zola any compiler I have worked with (MSVC, Dev-C++, gcc) will let you run code where not all control paths return a value.

Posted: Thu Mar 04, 2004 8:53 pm
by schick
probably the line has to be:

Code: Select all

if( irrDevice != 0 && irrDevice->getSceneManager()->getActiveCamera() != 0)

or just

if( irrDevice && irrDevice->getSceneManager()->getActiveCamera())


Posted: Sat Mar 06, 2004 1:39 am
by schick
ohh dear, i just realized whats going on... the && makes the if statement check both irrDevice and irrDevice->Func() but if irrDevice is NULL ,the irrDevice->Funct call will end in a SEGFAULT.

do something like

Code: Select all

if( irrDevice)
{
if( irrDevice->getSceneManager()->getActiveCamera())
//... do something
}

Posted: Sat Mar 06, 2004 3:00 am
by thesmileman
Thanks for the help Schick but I do not think you are correct.

For the purpose of speed C and C++ use lazy evaluation: if the left side of the && is false then there is no reason to check the right side because the entire result is false even if the right side is true.

The same is for ||: if the left side is true then the right side is not evaluted as the entire equation will be true regardless.

It more importantly makes many thing shorter and (at least to me) less confusing

For example this will never cause an error at least in C/C++ or any lazy evaluation language:

Code: Select all

int array[5];
if( 0 & array[100] ) 
   cout << "Hi!\n";
[/code]

Posted: Mon Mar 08, 2004 7:40 pm
by Luke923
thesmileman wrote:The problem is with this line

Code: Select all

if(irrDevice != 0 && irrDevice->getSceneManager()->getActiveCamera())
And Gorgon Zola any compiler I have worked with (MSVC, Dev-C++, gcc) will let you run code where not all control paths return a value.
I feel that Gorgon Zola might have a right to raise an issue. True, you can compile and run code with functions that do not return values dor every path; however, it appears that your SEGFAULTS are occurring when both irrDevice and getActiveCamera() return NULL, thus leaving no value for the function to return. If you put "return true;" at the bottom of the function, this should eliminate the SEGFAULT.

Just my $0.02f.

Posted: Mon Mar 08, 2004 7:59 pm
by TYLENOL
Hi,

my guess:

Code: Select all

irrDevice->getSceneManager() == NULL
(it's returning null).