Visual Studio Developer Center > Visual Studio Forums > Visual Studio Debugger > weird behaviour when debugging singleton object
Ask a questionAsk a question
 

Answerweird behaviour when debugging singleton object

  • Friday, July 03, 2009 1:22 PMBit01 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    hi
    i use a singleton object in my app (see below code, that's just a test class) . When i debug the application, the singleton object gets created at different times depending where i place the breakpoint and if debug step by step or not. Essentially i've placed a breakpoint in the instance property and inside construnctor.
    if i place a breakpoint on the calling instruction, or if i debug it step by step,  in the form load, the object gets created without hitting the inner breakpoints; while if i place the breakpoint before the calling instruction and let debugger run , inner breakpoints are hit correctly. The result of prints may help to explain:

    if run the debugger step by step i obtain
    "get - constructor "  (ignored breakpoints)   ->   " get - get - get "


    "get - constructor"  - without stopping on inner breakpoints
    "get"  - inner breakpoint , the constructor has already been called

    depending if run the debugger step by step or let him run once it has hit the get inner breakpoint.

    if i don't let debbugger stop on the calling instruction (only on object constructor and get breakpoints), it behaves correctly, obtaining the following messages:
    get - constructor
     
    This behaviour is a bug of visual studio 2008 debugger or there's some kind of debugging option i've enabled/disbled, i've to enable/disable?


     public class singleton
        {
            private static singleton _instance;
            //private static Object lockitm = new object();
            private int id;
    
            public int Id
            {
                get {
                    return id;
                }
            }
    
    
            public static singleton Instance
            {
                get {  //i've placed a breakpoint here<br/>
                    Console.WriteLine("get");
                    //lock (lockitm)
                    {
                        if (_instance == null)
                            _instance = new singleton();
    
                    }
                    return _instance;
                }
            }
    
            private singleton()
            {
                Console.WriteLine("constructor");<br/>
                id = new Random().Next();//i've placed a breakpoint here<br/>
            }<br/>
    <br/>
        }<br/>
    <br/>
    later in a form:<br/>
    <br/>
    private void Form1_Load(object sender, EventArgs e)<br/>
    {//i've placed a breakpoint here<br/>
                label1.Text = singleton.Instance.Id.ToString();<br/>
    }<br/>
    <br/>
    
    

Answers

  • Monday, July 06, 2009 3:45 AMRoahn LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Bit,

    What we are referring to is a huge topic which is called Funceval (Function Evaluation) in Visual Studio. Debugger commonly uses funceval to run ToString() and property getters. When we set a breakpoint in the middle of the evaluation, it's the debugger's policy to decide how to handle this. In VS2005, debugger handles "nested break states" and allow us to stop inside of the evaluation.

    We could do Funceval whenever we want, however, we need the thread at a GC safe point in order to avoid a deadlock.
    You may get more information about Rules of Funceval from this blog: http://blogs.msdn.com/jmstall/archive/2005/11/15/funceval-rules.aspx

    If we do not like this, we could go to Tools -> Options... -> Debugging -> General -> uncheck "Enable  property evaluation and other implicit function calls" and check "Step over properties and operators (Managed only)

    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!

All Replies

  • Monday, July 06, 2009 3:45 AMRoahn LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Bit,

    What we are referring to is a huge topic which is called Funceval (Function Evaluation) in Visual Studio. Debugger commonly uses funceval to run ToString() and property getters. When we set a breakpoint in the middle of the evaluation, it's the debugger's policy to decide how to handle this. In VS2005, debugger handles "nested break states" and allow us to stop inside of the evaluation.

    We could do Funceval whenever we want, however, we need the thread at a GC safe point in order to avoid a deadlock.
    You may get more information about Rules of Funceval from this blog: http://blogs.msdn.com/jmstall/archive/2005/11/15/funceval-rules.aspx

    If we do not like this, we could go to Tools -> Options... -> Debugging -> General -> uncheck "Enable  property evaluation and other implicit function calls" and check "Step over properties and operators (Managed only)

    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!