Thanks for the explanation Gordon.  I guess I wish there were pointers to
pointers in AS3 based on what you described.  I wasn't necessarily looking
to null out object a in my example, but just carry the pointer along.  The
following example is closer to what I'm getting at:

var a:Object = new Object();
var b:Object = new Object();

ChangeWatcher.watch(a, ...

....

a = b

I may set up the ChangeWatcher in one screen but make the change to a in
another.  Based on what you described how would I make sure that a
ChangeWatcher doesn't hold onto the reference?

Thanks,

Jaime

On 1/30/07, Gordon Smith <[EMAIL PROTECTED]> wrote:

 It sounds like you're thinking that a = null somehow wipes out the object
itself. It doesn't. It simply nulls out the variable 'a', which is a
different thing from the object that 'a' refers to.



Think of variables like 'a' and 'b' as "pointers" to objects.
(Technically, they're 4-byte areas of memory that store the memory address
where an object exists.) After executing the first two lines, you have two
different pointers pointing to the same object. After executing a = null,
the 'a' pointer no longer points to the object but the 'b' pointer still
does.



So now you might be wondering how you wipe out the object itself. You
can't do this directly; only the garbage collector can destroy an object. It
is allowed to do so whenever there are no references (pointers) to the
object. So you could indirectly wipe out your object by setting both 'a' and
'b' to something else. The object would then become eligible for garbage
collection at some indefinite time in the future.



- Gordon


 ------------------------------

*From:* [email protected] [mailto:[EMAIL PROTECTED] *On
Behalf Of *Jaime Bermudez
*Sent:* Tuesday, January 30, 2007 1:52 PM
*To:* [email protected]
*Subject:* [flexcoders] AS3 object reference changes?



I have a simple question that may be dumb but I find somewhat
interesting.  I have something similar to the following simplified AS3 code:

var a:Object = new Object();
var b:Object = a;

a = null;

-------------------------------------------

Why does object b hold on to a's object reference?  I would expect b to
become null.



Thanks,

Jaime

Reply via email to