Have you run this program with your debugger? The debugger should be able to tell you exactly where the problem is detected and you should be able to view the variables at the time of the crash.
...