Scott Dorman

ephemeral segment

  Home  |   Contact  |   Syndication    |   Login
  608 Posts | 11 Stories | 898 Comments | 51 Trackbacks

News


Post Categories

Image Galleries


Microsoft Store


Creative Commons License



Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

View blog authority

Add to Technorati Favorites

Windows Live Alerts

AddThis Social Bookmark Button

LinkedIn profile

Community Credit profile

The Code Project

Follow me on Twitter

Get Free Shots from Snap.com

Community Credit Hall of Fame

Get Feedghost

Xobni outlook add-in for your inbox



Support This Site

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

The Watch window (actually there are four different Watch windows you can use) is easily the most powerful aspect of the Visual Studio debugger and the underlying technology it uses actually surfaces in several places:

  • Watch
  • Autos
  • Locals
  • Quick Watch
  • “Data Tips” (the debugging tool tips that appear when you hover over a code element)

The thing that most people might not realize is that you can actually call methods in the Watch window. This is useful because it allows you to see data structures that might not otherwise display well and you can also have completely dynamic assertions.

Calling a method in the Watch window is as simple as adding the parenthesis and any parameters needed.

image

There are a few considerations to be aware of when doing this, however. They are:

  • The function (property or method) executes in the context of the debuggee.
  • Don’t do anything more than reading memory.
  • The method must execute in less than 20 seconds.
  • Breakpoints are not hit when called from the Watch window.

Another very useful trick is that the Watch window supports drag and drop from the code editor. Simply select the expression in the code editor and drag it to the watch window.

Just like you can execute functions in the watch window, you can execute them in the Value column of the Watch window as well. This allows you to easily change the value of a watched expression to a completely new instance of an object. In the Value column, simply enter “new SomeClass()” to set the value to a new instance of the SomeClass object.

From the Immediate window you can type in an expression to allocate on object

   1: ? SomeClass x = new SomeClass()

and then in the Value column enter the variable prefixed with a “$” sign. (In this example, $x.)

Finally, let’s take a look at the Make Object ID capability of the debugger. This is available from the Locals and Autos windows through the context menu. Simply select the object you are interested in and right click, then choose the Make Object ID. This will create a numeric ID for that object, which will be displayed after the value.

What is actually happening here is that it tells the debugger to watch a particular object in memory no matter where it goes. This is useful because if the actual object goes out of scope you can still watch it by using the object ID. If the object ID says “Can’t evaluate” then it means the object has been garbage collected. Object IDs can also be used in conditional breakpoints and as parameters to function calls.

For instance, if you wanted to know what GC generation an object is in, you can do so using Object IDs.

Technorati Tags: , ,
DotNetKicks Image
posted on Saturday, February 14, 2009 8:23 PM

Feedback

# re: Visual Studio 2008 Debugging – The Watch Window 2/25/2009 4:05 PM naveen
hi,

I am working on a samll program development using MS Visual Studio 2005. I ve written couple of lines of code, and during run time when i place a variable in the watch window, i get
"CXX0017: Error: symbol "integer" not found"
i am not sure what the problem is, but the program executes and works fine.but i will be into trouble when i try to debug the code on later part of development.

any suggestion would help.

naveen


# re: Visual Studio 2008 Debugging – The Watch Window 2/26/2009 2:16 PM naveen
hi,

answering my previous post, disabling the optimization options in the project and file properties solved the problem..

thanks
naveen
naveenkrm@gmail.com

# re: Visual Studio 2008 Debugging – The Watch Window 8/8/2009 9:26 AM biosys
hi! I closed window witch Watch ... where can I find it?

Thanks!

# re: Visual Studio 2008 Debugging – The Watch Window 9/10/2009 10:15 AM seed
right click on the variable and you'll see the option


# re: Visual Studio 2008 Debugging – The Watch Window 10/6/2009 3:23 AM controlsys
i have the same problem
where click on variable ?

# re: Visual Studio 2008 Debugging – The Watch Window 10/21/2009 4:05 PM William Prest
I am trying to add a variable to the watch window while my C# program is running, but the watch window is disabled and after selecting the variable and trying to drag it, nothing happens. Instead, I end up dragging a circle with a slash as if the operation not allowed. I have a breakpoint in the function. What am I doing wrong?

# re: Visual Studio 2008 Debugging – The Watch Window 11/20/2009 6:32 AM pcontrol
It's very interesting feature but I also hve problems with finding it.

# re: Visual Studio 2008 Debugging – The Watch Window 12/18/2010 6:25 AM wiiiktor
Watch Window - how to switch it on?

You run your app to be in the Debug mode. Then you go to the Debug toolbar (maybe you need to switch it on before). Then you find a drop-down menu and choose Watch1 window from there. (Probably there are other methods, this seems complicated but this how I did it).

# re: Visual Studio 2008 Debugging – The Watch Window 2/21/2011 11:59 AM Egor Kotov
How can i watch the hole value of pin_ptr<const wchar_t> variable , not the first letter only ?


# re: Visual Studio 2008 Debugging – The Watch Window 1/18/2012 6:48 AM soumya
How to get get add watch window?

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: