Friday, May 22, 2009

No source code available for current location

Such message hit us today. Unfortunately, none of standard solutions works.

Stepping through in VS gives "No source code [...]", running from OS shell ends the application with "This Program Has Performed an Illegal Operation [...]".

The code raising the message is nothing special:

   1: instance.Property1 = something;
   2: instance.Property2 = somethingelse;
   3: instance.Property3 = yetsomethingelse;

All properties of the class are inherited from a base class from an external assembly and this has been my first clue. I thought that for some reason the assembly containing the base class is not referenced properly, so that altough I can see all properties in IDE, an older version of the assembly is used during the debuggin session and the other version just does not contain "Property2".

Since "Property2" had just been added a build or two ago, I've been almost sure that this is the issue.

Unfortunately, the issue turned out to be completely unrelated to assembly versioning.

It turned out that the class has yet another property, "Property4" which has also been added a build or two ago, and the Property4 has been implemened incorrectly, causing the stack to overflow.

The runtime crash was then caused by the stack overflow when Property4 was executed.

Why then the Visual Studio has been raising the "No source code available [...]" when stepping through the call to Property2?

I have no darn idea. I can only imagine that Visual Studio tries to be smart and caches calls to properties just "in advance", in case you need them in a while. When stepping through Property2, Property4 has probably been executed to cache its value. And the stack overflow confused Visual Studio.

Does such issue always confuses Visual Studio then?

Yes. Just try it on following code:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   6: namespace ConsoleApplication51
   7: {
   8:     class Test
   9:     {
  10:         public int Property1 { get { return 1; } }
  11:         public int Property2 { get { return 2; } }
  12:         public int Property3 { get { return Property3; } }
  14:     }
  16:     class Program
  17:     {
  18:         static void Main( string[] args )
  19:         {
  20:             Test test = new Test();
  22:             var t1 = test.Property1;
  23:             var t2 = test.Property2;
  24:             var t3 = test.Property3;
  26:         }
  27:     }
  28: }

Correct behavior: put a breakpoint in line 22, run the code with debugging enabled, step to line 23 and move your move pointer over "test" in line 20. When you unfold the properties of the object, you'll see "Function evaluation was aborted" under "Property3".

Incorrect behavior: put a breakpoint in line 22, run the code with debuggin enabled, then step to liine 23 and to line 24 and then move your mouse over "test.Property3" in line 24. Wait a second or two and the code will eventually crash, ending the debugging session without any prompt or message.

I guess something related to the latter issue has caused the former issue for us.

No comments: