[ACCEPTED]-How to debug a deadlock?-deadlock

Accepted answer
Score: 10

What you did was the correct way. If Visual 19 Studio also deadlocks, that happens now 18 and then. It's just bad luck, unless there's 17 some other issue.

You don't have to run the 16 application in the debugger in order to 15 debug it. Run the application normally, and 14 if the deadlock happens, you can attach 13 VS later. Ctrl+Alt+P, select the process, choose debugger 12 type and click attach. Using a different set of 11 debugger types might reduce the risk of 10 VS crashing (especially if you don't debug 9 native code)

A deadlock involves 2 or more 8 threads. You probably know the first one 7 (probably your UI thread) since you noticed 6 the deadlock in your application. Now you 5 only need to find the other one. With knowledge 4 of the architecture, it should be easy to 3 find (e.g. what other threads use the same 2 locks, interact with the UI etc)

If VS doesn't 1 work at all, you can always use windbg. Download here: http://www.microsoft.com/whdc/devtools/debugging/default.mspx

Score: 4

I'd try different approaches in the following 18 order:

  • First, inspect the code to look for 17 thread-safety violations, making sure that 16 your critical regions don't call other functions 15 that will in turn try to lock a critical 14 region.

  • Use whatever tool you can get your 13 hands on to visualize thread activity, I 12 use an in-house perl script that parses 11 an OS log we made and graphs all the context 10 switches and shows when a thread gets pre-empted.

  • If 9 you can't find a good tool, do some logging 8 to see the last threads that were running 7 before the deadlock occurred. This will 6 give you a clue as to where the issue might 5 be caused, it helps if the locking mechanisms 4 have unique names, like if an object has 3 it's own thread, create a dedicated semaphore 2 or mutex just to manage that thread.

I hope 1 this helps. Good luck!

Score: 0

You can use different programs like Intel(R) Parallel 5 Inspector:

Such programs can show you places 4 in your code with potential deadlocks. However 3 you should pay for it, or use it only evaluation 2 period. Don't know if there is any free 1 tools like this.

Score: 0

Just like anywhere, there're no "Silver 16 bullet" tools to catch all the deadlocks. It 15 is all about the sequence in which different 14 threads aquire resources so your job is 13 to find out where the order was violated. Usually 12 Visual Studio or other debugger will provide 11 stack traces and you will be able to find 10 out where the discrepancy is. DevPartner 9 Studio does provide deadlock analysis but 8 last time I've checked there were too many 7 false positives. Some static analysis tools 6 will find some potential deadlocks too.

Other 5 than that it helps to get the architecture 4 straight to enforce resource aquisition 3 order. For example, layering helps to make 2 sure upper level locks are taken before 1 lower ones but beware of callbacks.

More Related questions