Debugging infinite loops with Visual Studio

Once in a while we run into a situation where the program run into infinite loop. Best symptom is the application seemed to be frozen. There are lot of different ways to approach it. One simple way is to put as much log as possible and dump it to a log file and have a UDP log viewer like log4net viewer and see what is happening. I like this approach, I call it layback debugging. This requires you need to know where this loop is happening and add as much log as possible. By the way there is nothing wrong with having more logs, but remember to make sure production is not noisy with all these logs. There are other brute force debugging methods like message boxes, putting break point and walking the code etc.,

Recently I ran into a situation where one of our Silverlight application fezzes after some time, there is no special pattern so we do not know exactly where it is happening but we had a ball park idea. To identify the problem area I used Visual Studio rather than logging or message box approach. So in our example, I am going to debug a Silverlight application which might be freezing after some work.

To do this, start the application and keep running it till it freezes. Now launch Visual Studio and open the solution of the application.

Goto Debug -> Attach to process (make sure ‘Show process in all sessions’ is checked) This will open a window and there scroll down and identify the process that you want to attach debugger to. In our case type will be ‘Siverligt’

Now that the frozen application is attached to the debugger, we can put break point and find out where the application is failing. But we do not know where is the current process is. If we do not know where the current process is how do we go about find out where the execution is? The answer is break at the current execution line, this can be achived by

Going to Debug -> Break All, this will stop at the current executing line but it will not stop the execution of the program, which means everything you could do in debugger mode is available to you at this time and you can resume execution by pressing F5 (Run). I would recommend not to use the first line where the program might be as the culprit causing it to go in the loop, rather, press F5 (Run) again and let it run and then break all again. See where is application breaks, keep doing it couple of times, this should give you a very good idea which part of the code might be the culprit for the infinite loops. The beauty of this is, it gives you the call stack as well, so you can see how the execution got to that state as well.

Visual Studio has ton features to help debug code and if you want to learn more about how to use these tools and features, head to Microsoft Debugging in Visual Studio.

Hope this help somebody. If there are better ways of debugging or anything related to this subject please send me a note, I love to hear it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s