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!
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!
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);
}
}
};
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.
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:
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