How do I debug my program when it hangs?

前端 未结 5 1335
面向向阳花
面向向阳花 2021-01-03 10:29

I have an application which takes measurements every second (I am running it in demo mode & generating random data, so the problem is not to do with reading from devices

5条回答
  •  借酒劲吻你
    2021-01-03 11:14

    Use the debugger to find out what your app is doing when it appears to hang.

    Run your program under the debugger. Let it run until it hangs. When it hangs, switch to the debugger and select "Debug: Break All" (or equivalent) to make the debugger freeze all threads and take control of the process.

    Now open the Threads view in the debugger and examine the stack traces of each thread in your program. Start with the main thread, obviously. Be sure to look back through the call stack for several calls to see if you recognize any of your code. If you find a stack trace that is in the middle of a loop, examine the local variables to see if your loop control variable has somehow slipped past the exit condition and put you into an infinite loop.

    If your stack traces indicate that every thread is blocked waiting for some external event, you may have a thread deadlock - thread A taking lock A, then trying to take lock B, while thread B is holding lock B and trying to take lock A. If you're not using threads in your program, this is less likely but still keep an eye out for it.

    If nothing odd jumps out at you after reviewing the stack traces of the threads, let the program run a few seconds more, then break into it again with the debugger and have another look around. See what's different in the stack traces.

    This should help you narrow down at least which bodies of code are involved in your hang.

提交回复
热议问题