java debugging - watch an out of scope variable
Asked Answered
R

2

6

When debugging a C/C++ application I can create a watch of an address cast to a pointer type and then view that contents of a structure even when it goes out of scope. Is it possible to achieve something like that in Java?
Let's say I'm in a method, I add a watch to something like "&this" and then when I leave the class method I can still see its contents even though it is out of scope?

I'm pretty sure the answer to my question is "no" so I'm mostly interested in "Why not?" explanation. Is this a JVM limitation? A JPDA limitation? Is there a better place to ask such a specialized question?

Raised answered 13/9, 2011 at 2:44 Comment(0)
L
1

In C/C++ you are watching (the area pointed to by) an arbitrary pointer, casting to that variable.

In Java there are no pointers. You can only watch a variable. Out of scope it does not exist.

Why would you want to watch it anyway? If you put a watch on it, it is watched properly when in scope.

Laciniate answered 13/9, 2011 at 2:54 Comment(2)
Objects exist out of the scope. Let's say I have a big graph of objects and a complicated algorithm processing it. I want to know at which stage the algorithm modifies a particular object in my graph. I would like to step through it keeping an eye on what is going on with the instane I'm interested in.Raised
Hmmm..yes, my bad. But there has to be some reference that is in scope from which that object is reachable. I guess you need to watch using that object which is scope, drilling down to the object of interest. I can see it is not convenient, and sometime not even supported.Laciniate
U
0

You cannot watch a variable that is not in scope.

You can however watch the same object, if you find another variable that points to it.

There is no point in watching an object that is not referenced anywhere (it cannot affect the program execution in any way, it cannot be changed anymore, it will be garbage-collected and the memory reused by something completely different).

The Java-level debugging tools still work on top of the JVM memory management, so you need a valid reference to an object, you cannot just peek at random memory areas.

Unseemly answered 13/9, 2011 at 2:55 Comment(2)
I don't think you understand the question. The object still has references to it, just not from the current class I'm stepping through at the moment. Let's say I'm stepping over 20 method calls and want to see which one of them modifies this objectRaised
I think you can only do that by finding a reference that exists throughout those 20 methods (maybe somewhere up the call stack) and then watch that one.Unseemly

© 2022 - 2024 — McMap. All rights reserved.