Click here to Skip to main content
15,867,568 members
Articles / All Topics

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (22 votes)
20 Oct 2010CPOL6 min read 91K   25   32
How to track an object which is Out of Scope while Debugging?

In Mastering in Visual Studio 2010 Debugging article, I have discussed about the basics of Object ID creation while debugging. I received some requests from some readers to explain the use of “Make Object ID” in detail. In this blog post, I am going explain how we can track an Object which is already out of scope using by creating a Object ID while debugging.

By using “Make Object ID” option, we are informing Visual Studio Debugger to keep track of that object no matter whether it’s within scope or out of scope for the current context. 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 ) uses 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 explore this with the help of the below code block:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CreateObjectIDDemoApps
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student> {
                new Student { Roll = 1, Name = "Abhijit", Address = "Hyderabad" },
                new Student { Roll = 2, Name = "Kunal", Address = "Pune" },
                 new Student { Roll = 3, Name = "Abhishek", Address = "Kolkata" },
                  new Student { Roll = 4, Name = "Rahul", Address = "Delhi" }
            };

            foreach (Student stud in students)
            {
                Console.Write(stud.ToString());
            }
        }

        class Student
        {
            public int Roll { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
            public override string ToString()
            {
                return "Roll : " + this.Roll.ToString() + 
		"\tName : " + this.Name + "\tAddress : " + this.Address + "\n";
            }
        }
    }
}

As per the above code, we have a list of Student object. I will show you how we can create a new object ID for any specific object and can track it even though it went out of scope. 

Creating Object ID

To make an Object Id, you have to view the object from Watch Window, then Right Click > Context Menu, select “Make Object ID”.

1

The watch window will display a number with the pound (#) sign .

2

The Object ID number will increment based on the object ID Creation. Which means for the next object ID the identification number will be like 2#,3#, etc. 

Using Object ID

You have already created an Object ID ( 1#) for a particular student object from list of Student objects. let’s have a look at how to use that object Id. You can easily type the object id in the watch window to get the details value of that object.

3

So as per the above image, you can see 1# object ID showing the details of the student object with Roll= 1, for which we have created that Object ID.

If you look into the details, you can see, though current break point showing the student details with Roll=3, still 1# Object ID pointing to Student Object with Roll 1, because of 1# object id now uniquely identifying one of the student objects.

4

Creating and Using Object ID for Multiple Objects

You can create and use the multiple Object ID using the same process that I have discussed. If you want to keep track of more than one object, you can use it.

5

When Object Goes Out of Scope?

Till now I have discussed how to create an Object Id and how to use them. Now let’s discuss about the actual use if Object ID. As I have already said,”Object Id is used to track any object even if the object is Out of Scope”. In my above example, both the object ID and the actual object was in the scope of current context. Let’s have a look at when the object goes out of scope and what is the behavior of Object ID at that time. 

To explore this, just put a break point outside of foreach statement, where “stud” object is not in the scope. Run the application and Create an object Id for the first object of student lists. Wait till when debugger hits the breakpoint next to foreach statement.

6

The above image is the quick summary of the track of object when the object goes out of scope. From the image you can see, “Stud” object is not getting evaluated as it is not in the current scope, but Object ID (1#) is still active which shows the actual values of the object for which ID was created. You can also click on the “Refresh” icon with the values of “stud” object to get the updated value. But, If you click on that icon, you will get the message “The name ‘stud’ does not exist in the current context” as shown below:

7

But you can get the same object values from the Object ID. Though your actual object goes out of scope, it’s not collected by the GC, Object ID still keep track of that object as the object is still present in memory. You will get the message “Can't evaluate object” once the actual object has been collected by the GC.

Working with “Out Of Scope” Object?

You can access any variables or methods from the object Ids as ((ClassName)ObjectID#).Properties or ((ClassName)ObjectID#).Method(). As per our example, we can access Roll for that Object as shown in below:

8

Similarly, we can access the same variables from “Immediate Window” as well.

9

Conditional Break Point with Object ID

You can also set conditional break points with the created object Ids. I have explained about how to use conditional break points over Mastering in Visual Studio 2010 Debugging – Conditional Breakpoint article. We have to use the same way that I have mentioned while discussing about the accessing Object ID.

10

The above conditions tell the Visual Studio Debugger that if the Object with Object ID 1# has Roll =1, then only hit the break point.

Note: While using Conditional breakpoint, you have to make sure you are creating the Object ID before debugger hits the conditional break points. Otherwise you will get the error message with “Object ID not found” as shown below:

11

Delete Object ID

You can delete the object using the similar way that you have used for creating an Object Id. Right Click on the Object Instance for which you have already created the Object Id, you will get “Delete Object ID” Option.

12

If you delete the Object Id with a specific number, it does not mean the next created ID will be the same ID that you have deleted. This means, let's say, you have 3 Object IDs 1#, 2# and 3#. You have deleted the object ID 2#. Now if you create another new Object ID, new ID will be 4# not 2#. Which indicates, one Object ID is used to uniquely Identify a particular object Instance, not for the same object again or for other object. The same ID can only be used once GC collects that object.

13

Summary

In this blog post, I have explained how we can use Object ID to keep track of an Out of Scope Object. By using “Make Object ID” option, we are informing Visual Studio Debugger to keep track of that object no matter whether it’s within scope or out of scope. When we create Object ID for an particular object, Visual Studio Debugger (CLR Debugging Services) uses 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.

Hope this will help you!

Thanks!
AJ


Filed under: Debugging, Tips and Tricks, Visual Studio

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

Comments and Discussions

 
QuestionInteresting! Pin
Nick Sagriotis14-Apr-14 21:43
Nick Sagriotis14-Apr-14 21:43 
GeneralMy vote of 5 Pin
Michael Haephrati20-Feb-13 5:26
professionalMichael Haephrati20-Feb-13 5:26 
GeneralThanks. I have really missed this feature. Pin
Mattias Högström14-Sep-12 0:30
Mattias Högström14-Sep-12 0:30 
GeneralMy vote of 5 Pin
Reiss1-Sep-11 2:47
professionalReiss1-Sep-11 2:47 
QuestionGood topic Pin
dave.dolan28-Jul-11 17:25
dave.dolan28-Jul-11 17:25 
GeneralMy vote of 5 Pin
JF201512-Jan-11 20:02
JF201512-Jan-11 20:02 
GeneralMy vote of 5 Pin
Emile van Gerwen25-Oct-10 21:59
Emile van Gerwen25-Oct-10 21:59 
GeneralRe: My vote of 5 Pin
Abhijit Jana29-Oct-10 20:56
professionalAbhijit Jana29-Oct-10 20:56 
GeneralMy vote of 5 Pin
thatraja21-Oct-10 19:34
professionalthatraja21-Oct-10 19:34 
GeneralRe: My vote of 5 Pin
Abhijit Jana21-Oct-10 20:25
professionalAbhijit Jana21-Oct-10 20:25 
GeneralRe: My vote of 5 Pin
thatraja21-Oct-10 20:31
professionalthatraja21-Oct-10 20:31 
GeneralMy vote of 5 Pin
Abhishek Sur21-Oct-10 1:19
professionalAbhishek Sur21-Oct-10 1:19 
GeneralRe: My vote of 5 Pin
Abhijit Jana21-Oct-10 1:21
professionalAbhijit Jana21-Oct-10 1:21 
GeneralMy vote of 5 Pin
Nish Nishant21-Oct-10 0:55
sitebuilderNish Nishant21-Oct-10 0:55 
GeneralRe: My vote of 5 Pin
Abhijit Jana21-Oct-10 1:20
professionalAbhijit Jana21-Oct-10 1:20 
GeneralRe: My vote of 5 Pin
Nish Nishant21-Oct-10 1:22
sitebuilderNish Nishant21-Oct-10 1:22 
GeneralMy vote of 5 Pin
NMehta8320-Oct-10 20:19
NMehta8320-Oct-10 20:19 
GeneralRe: My vote of 5 Pin
Abhijit Jana20-Oct-10 20:47
professionalAbhijit Jana20-Oct-10 20:47 
GeneralPretty Good Pin
gothic_coder20-Oct-10 19:59
gothic_coder20-Oct-10 19:59 
GeneralRe: Pretty Good Pin
Abhijit Jana20-Oct-10 20:48
professionalAbhijit Jana20-Oct-10 20:48 
QuestionTake a 5. Pin
Hiren solanki20-Oct-10 19:48
Hiren solanki20-Oct-10 19:48 
AnswerRe: Take a 5. Pin
Abhijit Jana20-Oct-10 20:46
professionalAbhijit Jana20-Oct-10 20:46 
GeneralRe: Take a 5. [modified] Pin
Hiren solanki20-Oct-10 21:03
Hiren solanki20-Oct-10 21:03 
GeneralRe: Take a 5. Pin
Abhijit Jana20-Oct-10 23:05
professionalAbhijit Jana20-Oct-10 23:05 
GeneralRe: Take a 5. Pin
Hiren solanki20-Oct-10 23:10
Hiren solanki20-Oct-10 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.