There is absolutely no guarantee that while an object's method is running the object is reachable, or that reference to it is on the call stack.
In practice, if the method is not inlined, and you are running on an X86 machine, and the JVM is using standard X86 calling conventions, it is quite likely that the "this" pointer will be pushed onto the call stack and remain there until the method returns. But in other contexts, the "this" pointer may equally well be passed in a register, and that register can be reused for something else after the last reference to "this" (i.e. after the access to x.index in the example). If the object (x in the example) is also dead in the caller, there is no reason to expect a reference to be saved anywhere. The object may happen to exist elsewhere in the caller's frame, but it's easy to construct examples for which that's not true if you're generating good quality code.
This is certainly a real issue on non-X86 machines, or if methods are inlined. I would guess that some X86 JVMs also pass arguments in registers, so it may matter even there, even if nothing is inlined. (Gcj/gij, the one I'm most familiar with, doesn't normally pass arguments in registers on X86, since it is constrained to follow the standard C/C++ ABI. Most JVMs are not.)
> -----Original Message-----
> From: David Holmes [mailto:email@example.com]
> Sent: Monday, March 24, 2003 4:21 AM
> To: Boehm, Hans; firstname.lastname@example.org
> Subject: RE: JavaMemoryModel: Another Java threading issue
> Hans Boehm wrote:
> > The C.finalize() method removes the corresponding data from
> > the A array.
> > Now consider a method C.foo() which may be the last call on
> > a C object. Assume that x of class C is accessed by a
> > single thread. Typically x.foo() will do something like:
> > x_index = x.index;
> > Operate on A[x_index];
> > The problem is that x may be unreachable after the read of
> > x.index. (Or potentially much earlier, if that memory read
> > was for example moved outside an enclosing loop.) Thus the
> > operation on A[x_index] may occur after x's finalizer has
> > run, and the data associated with x has been removed from A.
> Could you clarify what the problem is here. I don't see how an object
> can become unreachable whilst a method invocation upon it is in
> progress. There must be a reference to the object in the callers stack
> at least.
> David Holmes
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel
This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:42 EDT