Poser une questionPoser une question
 

Questiondebuger VS2008 bug?

  • vendredi 26 juin 2009 17:14zorank Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hello,

    please try this code and (a) step through it using F11 untill the code finishes, and (b) just remove the break point at the begining and let it run. The output is different. In the (a) case one has 

                d is null on entry
                d is not null on entry
                d is not null on entry
                d is not null on entry

    and in (b) case

                d is null on entry
               
    though in my opinion output should be same regardles which method one uses (being possibly bug in the VS2008 debugger). Here comes the code and the question at the end of it.

    using System;
    
    using System.Text;
    
    using System.Diagnostics;
    namespace StaticFieldsTest { class Program { static void Main(string[] args) { A a = new A(123); //set break point here and press F11 all the time untill program finishes B b = a.MyProperty; //at this stage output should read: // ... // 'StaticFieldsTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll' // d is null on entry // d is not null on entry // d is not null on entry // d is not null on entry // The thread 0x648 has exited with code 0 (0x0). // ... } } public class A { public long id;
    public A(long id) { this.id = id; } public B MyProperty { get { return StaticClass.GetProperty1(); } } } public class B { } public static class StaticClass { private static C d = null;
    private class C { private int i; public C(int i) { this.i = i; } } internal static B GetProperty1() { if (d == null) Debug.WriteLine("d is null on entry"); if (d != null) Debug.WriteLine("d is not null on entry"); d = new C(12345); return new B(); } } }

    Strange things happen depending on whether one lets the code run and when ones steps through it using F11. Also, if I run it with F11 when I enter the GetProperty1 method d field is already initialized with instance of a C class though it should not be since it is defined with "null" value and has never been accessed before! Somehow, debugger runs the code in GetProperty1 before giving me controll back. 

    Is this a bug or expected (documented) behavior?

    With best regards
    Zoran

    p.s. The code above comes from larger project and I have removed all irrelevant part to be able to focus on this behavior. (So code above might look silly). In reality the GetProperty1 method is much more complex and its workings do depend on the first line "if (d == null) ...".
    • Modifiézorank vendredi 26 juin 2009 17:20
    •  

Toutes les réponses

  • lundi 29 juin 2009 06:25Roahn LuoMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code

    Hello Zoran,

    Thanks for your feedback of our product!

    Based on my understanding, the issue could be described: the output result is not the same after we remove the breakpoint at the begining. What we expect is the same result.

    With my respect, do you upgrade the Visual Studio to the latest version?
    I did a test on my box with Visual Studio 2008 SP1, the detailed version information is:
    Microsoft Visual Studio 2008 Version 9.0.30729.1 SP and Microsoft .NET Framework Version 3.5 SP1
    and here's the detailed steps and results:
      1. create a new C# console application named StaticIssue
      2. paste the code snippet you provide. The whole code snippet is:

    using System.Diagnostics;
    namespace StaticIssue
    {
        class Program
        {
            static void Main(string[] args)
            {
                A a = new A(123);
                B b = a.MyProperty;
            }
        }
         public class  A
        {
            public long id;
            public A(long id)
            {
                this.id = id;
            }
            public B MyProperty
            {
                get { return StaticClass.GetProperty1(); }
            }
        }
        public class B
        {
        }
        public static class StaticClass
        {
            private static C d = null;
            private class C
            {
                private int i;
                public C(int i)
                {
                    this.i = i;
                }
            }
            internal static B GetProperty1()
            {
                if (d == null) Debug.WriteLine("d is null on entry");
                if (d != null) Debug.WriteLine("d is not null on entry");
                d = new C(12345);
                return new B();
            }
        }
    }
    


      3. press F11 and step into the code
         The result is:
    .....
    d is null on entry
    .....
    The thread 0x1934 has exited with code 0 (0x0).
    The thread 0x1b14 has exited with code 0 (0x0).
    The program '[2424] StaticIssue.vshost.exe: Managed' has exited with code 0 (0x0).

      4. remove the breakpoint and then step into the code again, and the result is:
    ....
    d is null on entry
    ....
    The thread 0x18ac has exited with code 0 (0x0).
    The thread 0x1ce0 has exited with code 0 (0x0).
    The program '[7964] StaticIssue.vshost.exe: Managed' has exited with code 0 (0x0).

    If we create a new project and follow the steps above, does the issue still occur?
    By the way, please visit our connect site if we need to submit a report or have some good suggestion.
    If it is an urgent issue, please contact our Help and Support center.

    Thanks a lot!


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
  • lundi 29 juin 2009 14:42zorank Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Hello Roahn,

    Many thanks for trying to help me out. Unfortunatlely problem stays.

    Based on your instructions I have installed SP1. This is what I have after installation (which is the same version as you indicated I should use):
    ============================

    Microsoft Visual Studio 2008
    Version 9.0.30729.1 SP
    Microsoft .NET Framework
    Version 3.5 SP1

    Installed Edition: Professional

    Microsoft Visual Basic 2008   91605-270-3145393-60623
    Microsoft Visual Basic 2008

    Microsoft Visual C# 2008   91605-270-3145393-60623
    Microsoft Visual C# 2008

    Microsoft Visual C++ 2008   91605-270-3145393-60623
    Microsoft Visual C++ 2008

    Microsoft Visual Studio 2008 Tools for Office   91605-270-3145393-60623
    Microsoft Visual Studio 2008 Tools for Office

    Microsoft Visual Web Developer 2008   91605-270-3145393-60623
    Microsoft Visual Web Developer 2008

    Crystal Reports    AAJ60-G0MSA4K-68000CF
    Crystal Reports Basic for Visual Studio 2008
    ================================

    I followed rest of you instructions, created project StaticIssue etc. Unfortunatelly, problem stays when I step through it with F11. The output is bellow:

    ================================================

    'StaticIssue.vshost.exe' (Managed): Loaded 'C:\...\StaticIssue\bin\Debug\StaticIssue.exe', Symbols loaded.

    Step into: Stepping over method without symbols 'object.Object'

    'StaticIssue.vshost.exe' (Managed): Loaded 'C:\...\System.Configuration.dll'

    d is null on entry

    Step into: Stepping over property 'StaticIssue.A.MyProperty.get'. To step into properties, go to Tools->Options->Debugging and uncheck 'Step over properties and operators (Managed only)'.

    d is not null on entry

    d is not null on entry

    Step into: Stepping over method without symbols 'Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly'

    The thread 0x1798 has exited with code 0 (0x0).

    Step into: Stepping over method without symbols 'System.Threading.ExecutionContext.Run'

    The thread '<No Name>' (0x1514) has exited with code 0 (0x0).

    The program '[6020] StaticIssue.vshost.exe: Managed' has exited with code 0 (0x0).
    =============================================================

    IMPORTANT: I noticed one interesting thing that might be important. When I step with F11 and pass the first line "A a = new A(123);" the StaticClass.GetProperty1() is already executed! (Though there is no reason for exectuting it at that point).

    Please take a look at the next example which exhibits same behavior but is much, much simpler.

    With best regards

    Zoran

    • Modifiézorank lundi 29 juin 2009 15:16
    • Modifiézorank lundi 29 juin 2009 15:13
    • Modifiézorank lundi 29 juin 2009 15:17
    • Modifiézorank lundi 29 juin 2009 16:09
    •  
  • lundi 29 juin 2009 15:52zorank Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    Hello again,

    I have managed to creat a super simple example of where the problem is. The code is as follows:

    ===============================================================
    using System.Diagnostics;
    
    namespace StaticIssue2
    {
        class Program
        {
            static void Main(string[] args)
            {
                A a = new A();
                Debug.WriteLine("About to enter a.MyProperty line...");
                Debug.WriteLine("...but no need to call CalculateMyProperty yet");
                int i = a.MyProperty;
            }
        }
        public class A
        {
            public int MyProperty
            {
                get
                {
                    Debug.WriteLine("Executing MyProperty");
                    return StaticClass.CalculateMyProperty();
                }
            }
        }
    
        public static class StaticClass
        {
            internal static int CalculateMyProperty()
            {
                Debug.WriteLine("Executing CalculateMyProperty");
                return 123;
            }
        }
    }
    
    If I just run the code then I get:

    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\Documents and Settings\...\StaticIssue2\bin\Debug\StaticIssue2.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    The thread 0xb00 has exited with code 0 (0x0).
    The thread 0x4f0 has exited with code 0 (0x0).
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\Documents and Settings\...\StaticIssue2\bin\Debug\StaticIssue2.exe', Symbols loaded.
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    About to enter a.MyProperty line...
    ...but no need to call CalculateMyProperty yet
    Executing MyProperty
    Executing CalculateMyProperty
    The thread 0x15ec has exited with code 0 (0x0).
    The thread 0x10ec has exited with code 0 (0x0).
    The program '[2180] StaticIssue2.vshost.exe: Managed' has exited with code 0 (0x0).
    
    

    Now If I step through it using F11:

    ...<br/>'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\Documents and Settings\zorank\My Documents\WORK\programs\TestProjects\StaticIssue2\bin\Debug\StaticIssue2.exe', Symbols loaded.
    Step into: Stepping over non-user code 'StaticIssue2.A.A'
    Step into: Stepping over non-user code 'System.Diagnostics.Debug.WriteLine'
    'StaticIssue2.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    About to enter a.MyProperty line...
    Step into: Stepping over non-user code 'System.Diagnostics.Debug.WriteLine'
    ...but no need to call CalculateMyProperty yet
    Executing MyProperty
    Executing CalculateMyProperty<br/>(COMMENT:two lines starting with Executing... have been printed before I even enter i = a.MyProperty; for some reason debugger executes getMyProperty before even entering a.MyProperty!!!<br/>Step into: Stepping over non-user code 'System.Diagnostics.Debug.WriteLine')
    Executing MyProperty
    Step into: Stepping over non-user code 'System.Diagnostics.Debug.WriteLine'
    Executing CalculateMyProperty
    Executing MyProperty
    Executing CalculateMyProperty
    Executing MyProperty
    Executing CalculateMyProperty
    The thread 0x1764 has exited with code 0 (0x0).
    The thread '<No Name>' (0x10a4) has exited with code 0 (0x0).
    The program '[1728] StaticIssue2.vshost.exe: Managed' has exited with code 0 (0x0).
    
    
    With F11 the debugger runs CalculateMyProperty three times! ...And the reason is that it executes getMyProperty three times and, interestingly, first time it exectues getMyProperty before I even enter execution of int i=a.MyProperty line!!!!

    With best regards
    Zoran


    • Modifiézorank lundi 29 juin 2009 15:56
    • Modifiézorank lundi 29 juin 2009 15:57
    • Modifiézorank lundi 29 juin 2009 16:05
    • Modifiézorank lundi 29 juin 2009 16:00
    • Modifiézorank lundi 29 juin 2009 15:59
    •  
  • mardi 30 juin 2009 03:25Roahn LuoMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Hello zorank,

    What's your Operating System?
    It seems that the SP1 is not properly installed, on my Windows XP box, it is displayed:

    Microsoft Visual Studio 2008
    Version 9.0.30729.1 SP
    Microsoft .NET Framework
    Version 3.5 SP1

    Installed Edition: Enterprise

    Microsoft Silverlight Projects 2008   91899-270-2988681-60946
    Microsoft Silverlight Projects 2008
    Version 9.0.30729.146

    Microsoft Visual Basic 2008   91899-270-2988681-60946
    Microsoft Visual Basic 2008

    Microsoft Visual C# 2008   91899-270-2988681-60946
    Microsoft Visual C# 2008

    Microsoft Visual C++ 2008   91899-270-2988681-60946
    Microsoft Visual C++ 2008

    Microsoft Visual Studio 2008 Team Explorer   91899-270-2988681-60946
    Microsoft Visual Studio 2008 Team Explorer
    Version 9.0.30729.1

    ......

    While, on my Windows Server 2008, it is displayed:

    Microsoft Visual Studio 2008
    Version 9.0.30729.1 SP
    Microsoft .NET Framework
    Version 3.5 SP1

    Installed Edition: Enterprise

    Microsoft Silverlight Projects 2008   91899-270-0341015-60741
    Microsoft Silverlight Projects 2008
    Version 9.0.30729.146

    Microsoft Visual Basic 2008   91899-270-0341015-60741
    Microsoft Visual Basic 2008

    Microsoft Visual C# 2008   91899-270-0341015-60741
    Microsoft Visual C# 2008

    Microsoft Visual C++ 2008   91899-270-0341015-60741
    Microsoft Visual C++ 2008

    Microsoft Visual Studio 2008 Tools for Office   91899-270-0341015-60741
    Microsoft Visual Studio 2008 Tools for Office

    ..........

    Will you please try to reinstall the SP1 and see if the issue still occur?

    Best regards,


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
  • mardi 30 juin 2009 07:49zorank Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    Hello Roahn,

    Many thanks for not giving up. I am running Windows XP Proressional version 2002 SP3.

    I have taken the VS2008 SP1 from

    http://www.microsoft.com/downloads/details.aspx?FamilyId=FBEE1648-7106-44A7-9649-6D9F6D58056E&displaylang=en

    I will gladly reinstall but I would like to check that the installation process is ineed the problem (I honestly doubt that the installation per se is the problem since it went automatically, there is no way I could do something differently synce installation program did not ask anything.) However few other things might be the prime suspects:

    (1) Is the link above correct? Is this where I should take SP1 from?

    (2) I have VS2005 on the machine. At the link above it says "If you have multiple Visual Studio products installed, you must upgrade all of them to SP1." I am not what is meant here by "Visual Studio Products", is it refering to VS2005 or various components of VS2008. Anyway, this should not be the issue since VS2005 is SP2. Who knows, if Sp1 is expecteced for some reason... Here are VS2005 details:

    Microsoft Visual Studio 2005
    Version 8.0.50727.762  (SP.050727-7600)
    Microsoft .NET Framework
    Version 2.0.50727 SP2
    
    Installed Edition: Professional
    
    Microsoft Visual Basic 2005   77626-009-0000007-41350
    Microsoft Visual Basic 2005
    
    Microsoft Visual C# 2005   77626-009-0000007-41350
    Microsoft Visual C# 2005
    
    Microsoft Visual C++ 2005   77626-009-0000007-41350
    Microsoft Visual C++ 2005
    
    Microsoft Visual J# 2005   77626-009-0000007-41350
    Microsoft Visual J# 2005
    
    Microsoft Visual Studio 2005 Tools for Applications   77626-009-0000007-41350
    Microsoft Visual Studio 2005 Tools for Applications
    
    Microsoft Visual Web Developer 2005   77626-009-0000007-41350
    Microsoft Visual Web Developer 2005
    
    Microsoft Web Application Projects 2005   77626-009-0000007-41350
    Microsoft Web Application Projects 2005
    Version 8.0.50727.762
    
    Crystal Reports    AAC60-G0CSA4B-V7000AY
    Crystal Reports for Visual Studio 2005
    
    
    Microsoft Visual Studio 2005 Professional Edition - ENU Service Pack 1 (KB926601)   
    This service pack is for Microsoft Visual Studio 2005 Professional Edition - ENU. 
    If you later install a more recent service pack, this service pack will be uninstalled automatically. 
    For more information, visit http://support.microsoft.com/kb/926601
    
    MySQL Connector/Net   5.1
    Data design and management tools for MySQL.  Copyright © 2007 MySQL AB <br/>
    


    (2) Could it be that some setting is set up in such a way that if static classes are referenced in properties debugger optimizes and executes them ahead of time?

    What do you think? Does any of the above sounds suspicious to you? Again, many thanks for not giving up!

    With best regards
    Zoran 
  • vendredi 3 juillet 2009 10:03zorank Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hello Roahn,

    I have reinstalled SP1 and problem stays. I thikn I will clean install VS2008. This turns this issue into "installation issue" so I have started a new thread under

    http://social.msdn.microsoft.com/Forums/en-US/vssetup/thread/c1ff19d4-f785-4962-b212-b917456e70af

    With best regards
    Zoran