Tracking Out of Scope Objects using DataTips while debugging in Visual Studio 2010

Visual Studio 2010 introduced  a nice new “DataTip pinning” features that enable you to better track of variable and expression while debugging. I am sure by this time you must be knowing what is “Data Tips”  and  different features of it.  In In this Post I am going to share a small tip which talks about tracking some out of scope object using DataTips while debugging.

Keeping track of out of scope object is not  new stuff in Visual Studio. You can use “Watch Window” to keep track of out of scope object while debugging by Creating a “Object ID”. Here is one of my old post which discussed about How to track an object which is Out of Scope while Debugging ?

Visual Studio 2010 DataTip also supports creating “Object ID” of objects. By using “Make Object ID” option we are informing Visual Studio Debugger to keep track of that object no matter it’s within scope or out of scope for the current context. Along with “DataTips” we can create “Object ID” either from Locals, Autos or from Watch Windows. Object ID is a integer number followed by a pound (#) sign. When we create Object ID for an particular object, Visual Studio Debugger ( CLR Debugging Services ) use an integer value to uniquely identify the object. This “Object ID” allows you to get the object details even if it is out of scope.

Let’s consider a simple example where we have a list of students and we are displaying them in a console.

image

On Run above code snippet will give output something like this

image

In the next step, we have set a breakpoint while displaying the information in console and Pinned the current Stud object which is the Students[0].

image

On the next iteration, pinned element will change  to Students[1] and similarly to Students[2] and Student[3] for rest other iteration.

image

Now, assume currently you are inspecting the object “Stud” in pinned DataTips which is referring “Students[1]” , at this point you want the value of  previous “stud” object which was point to “Students[0]. Well, don’t say that we have the value in Students list and we can check it from there, yes we can, but our objective is something different here.  Let’s focus on how we can retain the previous values of Stud object.

So now we are going to create an object ID for stud to keep the track.  How ? Well, with in DataTip, just right click on the object and select “Make Object ID

image

This will add  a number with the pound (#) sign with in value field of Data Tips. That #number is the unique identifier of the marked object.

image

By referring the object Id we can get the value of Object at any point of time during debugging no matter of the scope of the object.  This is very simple example, but think about you want to keep track of some object information which are in some local method scope.

What next ? Iterate to next element, DataTip will show the element of Students[1] .

image

So where is our previous “Stud” object. To get that value, right click on “DataTips” and select “Add Expression

image

This will create a placeholder for new elements with in DataTips.

image

Type 1# with in the new place holder and that’s all. Thumbs up You have the value of previous “Stud” objects.

image

Similarly you can create more “Object ID” to keep other variables as well. You can inspect that object in Immediate window as well

image 

On a side note, you can  also use Visual Studio Memory Window to check if there is something exists on the particular Address or not.  You can navigate to Memory Window from Debug > Windows > Memory .

image

Note : The Memory window is available only if address-level debugging is enabled ( Tools > Options > Debugging )

image

Before create the Object ID, if you try to find out the address, you will get “unable to evaluate the expression” message.

image

On creation of Object ID, you will get the memory details of the Object.

image

I will suggest to give a look at my previous article  for details discussion on “Watch window and Object ID Creation”

To Summarize, By using “Make Object ID” option we are informing Visual Studio Debugger to Keep track of that object no matter it’s within scope or out of scope. When we create Object ID for an particular object, Visual Studio Debugger ( CLR Debugging Services ) use an integer value to uniquely identify the object. This “Object ID” allows you to get the object details even if it is out of scope. If you received the message “Can’t evaluate the Object Values” while working with “Object ID”, it means that Object has been garbage collected. This is another way where you can identify that an Object has been garbage collected.

Light bulbRelated Post :

How to track an object which is Out of Scope while Debugging ?

10 Tips you should know about “Watch Window” While debugging in Visual Studio

How to use out of scope object with in Conditional Breakpoint ?

Hope this will help !

Cheers !

Aj

4 thoughts on “Tracking Out of Scope Objects using DataTips while debugging in Visual Studio 2010

  1. Tracking Out of Scope Objects using DataTips while debugging in Visual Studio 2010 « Abhijit’s World of .NET…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s