If you're lucky, you can run your code in the debugger until it hangs, and then stop the debugger to find the line of code violation. But if it were that simple, you probably would not ask for advice. :-)
Two strategies that can be used together are “divide and win” and “leave bread crumbs”.
Divide and Win: Comment on an increasingly significant portion of your code. If it still freezes, you have reduced the amount of code that can cause delays. Caution: in the end, you will comment on some code, and the program will not freeze. This does not mean that the last bit of code is necessarily responsible for freezing; it is somehow connected. Return it and comment on something else.
Leave the bread crumbs: Make your program, tell where it is and what it does when it is running. Display a message, add to a log file, create sound, or send a packet over the network. Is the execution path as you expected? What was the last thing he did before he froze? Again, keep in mind that the last message may have come from a different thread than the one responsible for freezing the program, but as you approach this reason, you will adjust what and where the code is written.
source share