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.

Windows Phone over Starwars MMO

After a long wait, I was finally able to login and play Star wars MMO, I was so psyched. I logged in and created my first Sith toon then I logged out since I have to go and learn and do some cool stuff in Windows Phone 7, how about that? So here I am at Microsoft and started my Windows Phone 7 application that I wanted to develop for some time. I never get time to do Windows Phone since I was doing few other things in my spare time. So here are the things, I got recommended by Microsoft to get started on Windows Phone development and thought I share it with you all and as a book mark for my future reference.

Code Samples

Silverlight Toolkit

“How do I” videos

Design Resources

Download free e-book for Windows Phone 7

Chirs is hosting the event and he has a fantastic blog and resources for Windows Phone development. I have been using Jounce for all my Silverlight development so obviously I decided to used Ultralight.MVVM. I keep you all posted on my progress on the application that I am developing.

Component One Silverlight FlexGrid Tip 5 (Group Summary Row positioning)

Continuing on from the previous tip on grouping, we go one step further. When we generate the grouping, Component One provides couple of options to how to render them. Well there are 3 to be precise. Component One have a property called ‘GroupRowPosition’. Which lets the developer to position the group row on top of the group or below the group or don’t show the group altogether.

1. GroupRowPosition=’AboveData’

<c1:C1FlexGrid Name="flexGrid" ItemsSource="{Binding PeopleCollection}" AutoGenerateColumns="False" GroupRowPosition="AboveData" >

As the name suggests, on UI rendering, C1 will place the group summary information above the data as shown below

image

If you do not provide this option in the grid, this is the default behavior.

2. GroupRowPosition=’BelowData’

<c1:C1FlexGrid Name="flexGrid" ItemsSource="{Binding PeopleCollection}" AutoGenerateColumns="False" GroupRowPosition="BelowData" >

This forces the summary row to show at the bottom of the group row. This is similar to Excel, when you group, you have an option to render the summary row at the bottom.

image

3. GroupRowPosition=’None’

<c1:C1FlexGrid Name="flexGrid" ItemsSource="{Binding PeopleCollection}" AutoGenerateColumns="False" GroupRowPosition="None" >

Not sure why you would do that but it is available. With this option the group row is removed but the grouping is still in place.

image

If you are requirements needs lots of grouping and want to create grouping and look and feel like Excel, please have a loot at C1FlexGridExcel control which is derived off of C1FlexGrid control. Which by fault look like Excel.

Thoughts on Unit Testing

Yesterday I had a great conversation with one of my friend about few things one of them was Unit testing. He is a big proponent of Unit Testing. Good unit testing (see I did not write more unit testing) could  help identify and resolve the bugs in the code very early. This also helps code maintenance. I use unit testing where ever and when ever possible. I will not sit and break my head over how will I do unit test when I develop form over data patterns. On the flip side when you develop a program that involves some kind of logic that manipulate data then for sure you need to identify the components and test it.

This was one of my friend example, why would we need unit testing, lets assume _a and _b are variable and instead of doing if (_a == _b) if you would do it, if (_a = _b) then you have problem. If you would write an unit test for it, you will catch it early enough. On the side note, this condition will not even compile in C#, because compiler will complain, it can not convert integer to bool implicitly. Anyway, you got the point.

One very import point I want to make is how many unit test you write? Write enough to do good code coverage. If you have a method which takes bunch of parameters and you are asserting against some values, in older version of NUnit, you would write individual methods for each assertion. With latest version of NUnit you can write the single test and decorate it with all the assertion saves lots of code duplication. It also make code maintenance easy. So please check out latest version of NUnit.

If you are lazy or do not want to do unit test yet you need to do please your boss, here is a short cut, use Microsoft PEX. In this, you point a method and PEX will create all the possible scenario test cases with all possible input parameters.

Now that I am doing more and more Silver Light + RIA, I need to do more reading on how I can test the code good. Any of you are using testing for these platform, drop me a mail.

Technorati Tags: ,