Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

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)
            {
                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)

Share

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 : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide
Follow on   Twitter

Comments and Discussions

 
QuestionInteresting! PinmemberNick Sagriotis14-Apr-14 22:43 
GeneralMy vote of 5 PinmvpMichael Haephrati מיכאל האפרתי20-Feb-13 6:26 
GeneralThanks. I have really missed this feature. PinmemberMattias Högström14-Sep-12 1:30 
GeneralMy vote of 5 PinmemberReiss1-Sep-11 3:47 
QuestionGood topic Pinmemberdave.dolan28-Jul-11 18:25 
GeneralMy vote of 5 PinmemberJF201512-Jan-11 21:02 
GeneralMy vote of 5 PinmemberEmile van Gerwen25-Oct-10 22:59 
GeneralRe: My vote of 5 PinmvpAbhijit Jana29-Oct-10 21:56 
GeneralMy vote of 5 Pinmemberthatraja21-Oct-10 20:34 
GeneralRe: My vote of 5 PinmvpAbhijit Jana21-Oct-10 21:25 
GeneralRe: My vote of 5 Pinmemberthatraja21-Oct-10 21:31 
GeneralMy vote of 5 PinmvpAbhishek Sur21-Oct-10 2:19 
GeneralRe: My vote of 5 PinmvpAbhijit Jana21-Oct-10 2:21 
GeneralMy vote of 5 PinmvpNishant Sivakumar21-Oct-10 1:55 
GeneralRe: My vote of 5 PinmvpAbhijit Jana21-Oct-10 2:20 
GeneralRe: My vote of 5 PinmvpNishant Sivakumar21-Oct-10 2:22 
GeneralMy vote of 5 PinmemberNMehta8320-Oct-10 21:19 
GeneralRe: My vote of 5 PinmvpAbhijit Jana20-Oct-10 21:47 
GeneralPretty Good Pinmembergothic_coder20-Oct-10 20:59 
GeneralRe: Pretty Good PinmvpAbhijit Jana20-Oct-10 21:48 
QuestionTake a 5. PinmemberHiren Solanki20-Oct-10 20:48 
AnswerRe: Take a 5. PinmvpAbhijit Jana20-Oct-10 21:46 
GeneralRe: Take a 5. [modified] PinmemberHiren Solanki20-Oct-10 22:03 
GeneralRe: Take a 5. PinmvpAbhijit Jana21-Oct-10 0:05 
GeneralRe: Take a 5. PinmemberHiren Solanki21-Oct-10 0:10 
GeneralRe: Take a 5. PinmvpAbhijit Jana21-Oct-10 0:17 
GeneralMy vote of 5 Pinmembermadhukk20-Oct-10 20:18 
GeneralRe: My vote of 5 PinmvpAbhijit Jana20-Oct-10 21:49 
GeneralMy vote of 5 Pinmembersashidhar20-Oct-10 18:34 
GeneralRe: My vote of 5 PinmvpAbhijit Jana20-Oct-10 21:50 
GeneralMy vote of 5 PinmentorKunalChowdhury20-Oct-10 17:12 
GeneralRe: My vote of 5 PinmvpAbhijit Jana20-Oct-10 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web02 | 2.8.141216.1 | Last Updated 20 Oct 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid