views:

88

answers:

5

I have a application which randomly freezes, including the IDE and it's driving me mad. That makes me wonder:

What's a good general strategy for finding the cause of random freezes?

+1  A: 

I would install the UserDump tool, and follow these instructions for generating a user dump of the application....

Once you have the user dump, you can use WinDbg, or cdb to inspect the threads, stacks, and locks, etc.

Often I find hangs are caused by locked mutexes or things like that.

John Weldon
+2  A: 

You're probably doing things in the UI thread when you shouldn't be.

Noon Silk
+1 for 'occams' triage' :)
John Weldon
That could be possible. Is there any way to better pinpoint the exact cause?
DR
+3  A: 

If you're lucky, you can run your code in a debugger until it freezes, then stop the debugger to find the offending line of code. But if it were that easy, you probably wouldn't be asking for advice. :-)

Two strategies that can be used together are to "divide and conquer" and "leave bread crumbs."

Divide and conquer: Comment out increasingly larger portions of your code. If it still freezes, you've reduced the amount of code that might be responsible for causing the freeze. Caveat: eventually you'll comment out some code and the program will not freeze. This doesn't mean that last bit of code is necessarily responsible for the freeze; it's just somehow involved. Put it back and comment out something else.

Leave bread crumbs: Make your program tell you where it is and what it's doing as it executes. Display a message, add to a log file, make a sound, or send a packet over the network. Is the execution path as you expected? What was the last thing it was doing before it froze? Again, be aware that the last message may have come from a different thread than the one responsible for freezing the program, but as you get closer to the cause you'll adjust what and where the code logs.

Adam Liss
+1 for "poor man's debugging".
Jason Orendorff
The question was tagged "language-agnostic" so I didn't want to make any assumptions about the platform or tools. And despite all the bad press, you can go a long way with printf() and some cleverness.
Adam Liss
+6  A: 

If you are wanting to check from outside of a running app then I would potentially use the sysinternals.com toolset from Mark Russonivich, the perfmon tool allows you to trace file / registry access and check the trace for delays - and what is being accessed at that time. It will show the DLL call stack at that time with the right symbols can is useful for debugging problems external to an application that are causing delays. (I've used it to find out that an I/O filter associated to a security suite was the reason an application was piccking up a number of 1.5sec delays.)

Andrew
Agree with Andrew. Sysinternals tools is the easiest and fastest way to identify portion of stack where your app is freezing
RocketSurgeon
A: 

The good general strategy is, run the program until it hangs. Then attach a debugger to it and see what's going on. In a GUI program, you're most interested in what the UI thread is doing.

You say the application hangs the IDE. This isn't supposed to happen, and I imagine it means the program is putting so much strain on the OS (perhaps CPU load or memory) that the whole system is struggling.

Try running it until it hangs, going back to the IDE, and clicking the Stop button. You may have to be really patient. If the IDE is really permanently stuck, then you'll have to give more details about your situation to get useful help.

Jason Orendorff