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.
On Run above code snippet will give output something like this
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.
On the next iteration, pinned element will change to Students and similarly to Students and Student for rest other iteration.
Now, assume currently you are inspecting the object “Stud” in pinned DataTips which is referring “Students” , at this point you want the value of previous “stud” object which was point to “Students. 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”
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.
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 .
So where is our previous “Stud” object. To get that value, right click on “DataTips” and select “Add Expression”
This will create a placeholder for new elements with in DataTips.
Type 1# with in the new place holder and that’s all. You have the value of previous “Stud” objects.
Similarly you can create more “Object ID” to keep other variables as well. You can inspect that object in Immediate window as well
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 .
Note : The Memory window is available only if address-level debugging is enabled ( Tools > Options > Debugging )
Before create the Object ID, if you try to find out the address, you will get “unable to evaluate the expression” message.
On creation of Object ID, you will get the memory details of the Object.
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.
Related Post :
Hope this will help !