Welcome to MSDN Blogs Sign in | Join | Help

Proactive vs Reactive tools: Evaluating Properties

Proactive vs Reactive tools: Prop Eval

 

One core principle for a debugger is that it should affect your program as little as possible.  The reason is pretty straightforward.  If your program runs differently under the debugger, the problem you are trying to solve might not reproduce.  This is the underlying reason why all debugger devs say FuncEval is Evil.  There are, however, a host of useful features that are in direct conflict with this principle.  Automatically evaluating properties is one such feature.  A property is really a pair of methods in your program.  It’s not a good idea for a property read to have a side effect.  However the no-side effect rule usually pertains to the abstract state of your object.  Take this for example:

 

using System;

class MyBag

{

      System.Collections.Hashtable m_items;

      public System.Collections.Hashtable MyItems

      {

            get

            {

                  lock(this)

                  {

                        if (null == m_items)

                        {

                              m_items = new System.Collections.Hashtable();

                        }

                  }

                  return m_items;

            }

      }

      static void Main(string[] args)

      {

            MyBag c = new MyBag();

      }

}

 

This is arguably a reasonable use for a property.  The abstract state of c.MyItems is an empty list, in both the case where m_item is null and where m_item is an empty list.  The real state however obviously changes if you read the property. 

 

?c

{MyBag}

    m_items: null

    MyItems: Count = 0

?c

{MyBag}

    m_items: Count = 0

    MyItems: Count = 0

 

Typically it is the real state that you care about when debugging, not the abstract one.    Should the debugger be evaluating that property?  You can turn off automatic property evaluation in the VS debugger (look under tools/options/debugging/General for “allow property evaluation in variables windows”). 

If you are stopped at if (null == m_items) line above and FuncEval the property what happens?  Why?

 

Published Monday, February 16, 2004 9:58 AM by SteveJS
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Proactive vs Reactive tools: Evaluating Properties

Is the debugger really so simple that it will lock itself up like this?
Monday, February 16, 2004 11:03 AM by Jerry Pisk

# re: Proactive vs Reactive tools: Evaluating Properties

I'm guessing you didn't try it. I'd respond in more depth but I don't want to give it away so soon.
Monday, February 16, 2004 11:34 AM by Steve Steiner

# null == m_items

It's funny to see C++ coding style persisting even after it's not needed anymore ;-)
Since m_items = null (common typo) won't compile in C#, why not use the simpler notation?
Monday, February 16, 2004 2:04 PM by Dumky

# re: Proactive vs Reactive tools: Evaluating Properties

Good habits die hard! I primarily code in C++, so it's a lot easier for me to keep the habit of the variable on the right when comparing to a constant, then to switch back and forth depending on what language I'm using. When C# becomes my primary dev language I'll need to adjust lots of habits to make sure they are still worthwhile. This one is certainly harmless in C#, and is a good habit in C++.
Monday, February 16, 2004 2:14 PM by Steve Steiner

# re: Proactive vs Reactive tools: Evaluating Properties

I haven't tried it, my VS will only work with unmanaged code. What will happen? The debugger will evaluate the property on a different thread than the one it interrupted so it should be stuck waiting for the lock, shouldn't it?
Monday, February 16, 2004 5:56 PM by Jerry Pisk

# re: Proactive vs Reactive tools: Evaluating Properties

In this specific case the debugger does evalute it on the same thread. It can successfully evalute the property since System.Threading.Monitor allows code executing on the same thread to obtain the lock.
Monday, February 16, 2004 6:19 PM by Steve Steiner

# re: Proactive vs Reactive tools: Evaluating Properties

If it evaluates on the same thread than it will work (to a point, you may end up creating it twice depending on where the debugger stopped the execution) since acquiring a lock on a thread that already has that lock will succeed.

on the other hand I would be interested in details on how exactly it's done, since the thread is stopped by the debugger - does the debugger dump the state info (registers, instruction pointers, stack and so on), restores the execution to the property getter, executes it and than restores the thread to the previous state hoping there were no side effects? What if there was a break point in the executed code? Would it break?
Monday, February 16, 2004 8:53 PM by Jerry Pisk

# re: Proactive vs Reactive tools: Evaluating Properties

Exactly! Depending on where your instruction pointer is located Prop Eval can alter the state of your debuggee. That is the real problem. The only way to fix that issue is to not do the Prop Eval.
<P>
Those are good questions and deserve a full blog topic. Which I may do - or convince someone else to do.
<P>
Yes you have to deal with breakpoints being hit. Would it break? That depends on if we want it to or not. In Whidbey PDC managed code has a nested break state feature, where you can break in a Func Eval. You don't break for properties though since the func eval is implicit rather than explicit user action.
Tuesday, February 17, 2004 8:25 AM by Steve Steiner

# Debuggers aren't supposed to change behavior

Saturday, October 02, 2004 2:48 AM by Mike Stall's .NET Debugging Blog

# Debuggers aren't supposed to change behavior

Saturday, October 02, 2004 11:51 AM by Mike Stall's .NET Debugging Blog

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker