Bug in the debugger?

J

Jason

This is an offshoot of my post "c# XOR equals (^=) bug?".

While I was working on the code for that problem, I think
I found a problem with the VS.NET debugger. That has to
do with the quick evaluation tooltips while debugging.

Here is the basic code to use for debugging and to
illustrate this "problem.":

//--- start code
using System;

class Class1
{
[STAThread]
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("before swap");
Console.WriteLine("x: {0}", x);
Console.WriteLine("y: {0}", y);
Swap(ref x, ref y);

Console.WriteLine("\nafter swap");
Console.WriteLine("x: {0}", x);
Console.WriteLine("y: {0}", y);
}

public static void Swap(ref int x, ref int y)
{
x ^= y;
y ^= x;
x ^= y;
}
} //end code

When debugging, highlighting an expression like "x ^= y"
with the mouse
and then hovering over the operator will display a
tooltip with the answer. However the original values
change and from that point on the values may not be
correct!

To reproduce in the code from above:

1. Load the above code into VS.NET. I used 2003.

2. Put a breakpoint on the first of the three lines of
the triple XOR swap.

public static void Swap( ref int x, ref int y)
{
BP--> x ^= y;
y ^= x;
x ^= y;
}

3. run the code in debug mode. the program will stop on
the breakpoint.

4. now, without advancing (no F10), highlight the one of
the expressions with the mouse and then hover over the
operator (^=) in that line. The tooltip will show the
results of the evaluation, but now the values in x and y
are changed permanently.

NOTE: when highlighting, make sure not to highlight the
semicolon at the end of the line or the tooltip won't
show.


Jason P.
..
 
J

Jason

I think that I found this not to be a bug. I think that
this must be part of the deal with the Quick watch
evaluation. I'm still looking for the docs to explain
this in detail.
 
J

Jason

I think I found something to explain it.

The MSDN library has a page in the VS.NET docs:

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/vsdebug/html/vcoricsharpexpressionsindebugger.asp

Under the section titled "Function Evaluation" there is a
possible explanation:

"...Evaluating a function in the debugger actually
calls and executes the code for that function. If the
function has side effects, such as allocating memory or
changing the value of a global variable, evaluating the
function in a debugger window will change the state of
your program, which can produce unexpected results..."

Maybe the quick watch/intellisense is evaluating the
operator^= in the code. In which case all this makes
sense.

Can anyone confirm?

Jason
 
J

Jason

I think that I found it. It's called DataTips, but there
is not much information about DataTips in the docs.

question is this: Why would the DataTips cause the actual
evaluation of the code and not just a temporary
evaluation that doesn't affect the variables?

From MSDN Docs
----------
DataTips
One of the least obtrusive tools is the DataTips pop-up
information box. When the debugger is in break mode, you
can view the value of a variable within the current scope
by placing the mouse pointer over the variable in a
source window. A DataTips pop-up box appears. To view the
value of an expression, select the expression. DataTips
pop-up information is not available for expressions
outside the current scope, invalid expressions (such as
division by zero), or expressions that involve function
evaluation. For more information, see Expressions in the
Debugger.


Jason
 
J

Jason

the final truth!!!
From the MSDN pages on Quick Watch and Debugging Basics

"...Note
Some expressions have side effects that change the value
of a variable or otherwise change the state of your
program when they are executed. Evaluating an expression
in the QuickWatch dialog box will have the same effect as
executed the expression in your code. This can produce
unexpected results if you do not pay attention to the
side effects of the expression.
Tip In Visual Studio, you can get a quick look at a
variable's value by placing the cursor over the variable.
A small box called a DataTip will appear showing the
value..."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top