Click here to Skip to main content
12,398,537 members (57,699 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


24 bookmarked

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

, 20 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)

        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”.


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


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.


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.


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.


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.


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:


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:


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


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.


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:


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.


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.



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!


Filed under: Debugging, Tips and Tricks, Visual Studio


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


About the Author

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

Blog :
Web Site :
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

You may also be interested in...

Comments and Discussions

QuestionInteresting! Pin
Nick Sagriotis14-Apr-14 21:43
memberNick Sagriotis14-Apr-14 21:43 
GeneralMy vote of 5 Pin
Michael Haephrati מיכאל האפרתי20-Feb-13 5:26
mvpMichael Haephrati מיכאל האפרתי20-Feb-13 5:26 
GeneralThanks. I have really missed this feature. Pin
Mattias Högström14-Sep-12 0:30
memberMattias Högström14-Sep-12 0:30 
GeneralMy vote of 5 Pin
Reiss1-Sep-11 2:47
memberReiss1-Sep-11 2:47 
QuestionGood topic Pin
dave.dolan28-Jul-11 17:25
memberdave.dolan28-Jul-11 17:25 
GeneralMy vote of 5 Pin
JF201512-Jan-11 20:02
memberJF201512-Jan-11 20:02 
GeneralMy vote of 5 Pin
Emile van Gerwen25-Oct-10 21:59
memberEmile van Gerwen25-Oct-10 21:59 
GeneralRe: My vote of 5 Pin
Abhijit Jana29-Oct-10 20:56
mvpAbhijit Jana29-Oct-10 20:56 
GeneralMy vote of 5 Pin
thatraja21-Oct-10 19:34
memberthatraja21-Oct-10 19:34 
GeneralRe: My vote of 5 Pin
Abhijit Jana21-Oct-10 20:25
mvpAbhijit Jana21-Oct-10 20:25 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 20 Oct 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid