Event Reciever occasionally crashes (rarely)

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
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Event Reciever occasionally crashes (rarely)

Post 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); 
      }
   }
};
schick
Posts: 230
Joined: Tue Oct 07, 2003 3:55 pm
Location: Germany
Contact:

where?

Post by schick »

On which line is the SEGFAULT ?
Gorgon Zola
Posts: 118
Joined: Thu Sep 18, 2003 10:05 pm
Location: switzerland

Post by Gorgon Zola »

strange it compiles at all. Not every execution path returns a value!
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Post 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.
schick
Posts: 230
Joined: Tue Oct 07, 2003 3:55 pm
Location: Germany
Contact:

Post 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())

schick
Posts: 230
Joined: Tue Oct 07, 2003 3:55 pm
Location: Germany
Contact:

Post 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
}
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Post 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]
Luke923
Posts: 59
Joined: Wed Nov 05, 2003 5:26 am

Post 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.
"Object-oriented programming is an exceptionally bad idea which could only have originated in California."
- E.W. Dijkstra
TYLENOL
Posts: 3
Joined: Mon Jan 19, 2004 10:46 am
Location: Brasil
Contact:

Post by TYLENOL »

Hi,

my guess:

Code: Select all

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