Luca Carlon give you good and fairly popular advice -- use debugger. In your case, you can get a lot of information about debugging from official documentation.
When you run debug of your program, you must replay situation when your program falls: you must manually start screensaver, or hurry up screensaver to start.
When your program fails -- get back-trace, and try to understand, what was wrong.