I'm not really familiar with technical specifications on how AS manages
argument references. However, I believe this has more to do with OO Paradigm
than with AS.

Let's say this code is in a myClass Class. This makes 'obj' a myClass
instance variable. This makes the object referenced by 'obj' accesible by
every method for every myClass instance.

Since creteObject(), checkObject() and deleteObject() are myClass' instance
methods, they can use this variable.

BUT, in the code you posted, checkObject() and deleteObject() each have a
parameter, and (I understand) AS handles parameters by creating a local
variable for the method; it's scope is the method and not the class. This
makes the code:

deleteObject(objParam:Object){
objParam = null;
}

equivalent to:

deleteObject(){
var objParam : Object;
objParam = obj;
objParam = null;
}

This is, exactly as it reads:
1) create an Object-type variable called objParam.
2) make objParam refer to the object referenced by instance variable called
'obj'.
3) make objParam refer to the null object.

As can be seen, the reference to 'obj' is never affected. And the object
itself is not GC'ed because it is still being referenced-to by obj instance
variable.

The proper approach here would be to make obj refer to null.

Hope it helped :)

Guido.

On 6/7/07, Jeffry Houser <[EMAIL PROTECTED]> wrote:


I think you missed his point.

He sent in obj as a parameter to a function. So objParam should be a
reference to obj; and nulling one should definitely null the other.

I do notice that the functions in the example are not declared as
functions with the "public function" keywords. Could that be a
contributing factor to the problem?

Roman Protsiuk wrote:
>
>
> objParam is not the same reference to created object as obj. Whenever
> you set objParam to null it becomes null (try checking it out in your
> deleteObject method). But who said the obj should become null? To
> prepare obj for garbage collection you should set obj to null.
>
> R.
>
> On 6/7/07, *Ravi Kumar Gummadi* <[EMAIL 
PROTECTED]<ravigummadi%40ivycomptech.com>
> <mailto:[EMAIL PROTECTED] <ravigummadi%40ivycomptech.com>>>
wrote:
>
>
> Hi all
>
> How does flex handle parameter passing, as I understand, it is by Pass
> by reference. But I was having some memory leaks and playing around a
> few tweaks and the following snippet completely took me by surprise.
>
> // CODE START
>
> <script>
>
> public var obj:Object;
>
> createObject(){
>
> obj = {test:"1234",test2:"5678"};
> }
>
> checkObject(objParam:Object){
> trace(objParam);
> }
>
> deleteObject(objParam:Object){
> objParam = null; // I tried objParam = undefined as well
> }
>
> </script>
>
> <mx:Button id="create" click="createObject()" />
> <mx:Button id="check" click="checkObject(obj)" />
> <mx:Button id="delete" click="deleteObject(obj)" />
>
> // END
>
> Now 1. I created the object by click on create
> 2. Then check for the existence of it (Traces [object object].. This
> is fine)
> 3. Then clicked delete.
> 4. Then again click on check... (Traces [object] [object]
> !!!!!!!!!!!!!!!!!!!!! But since its passed by reference it should be
> NULL)
>
> Somewhere I read that it setting to NULL changes the reference count,
> does that mean in each of my function I need to set all the params to
> null at the end of it so that refercne count is reduced and raady for
> garbage collection!! ( That doesn't quite a sense !!)
>
> Regards
> Ravi
>

--
Jeffry Houser, Technical Entrepreneur, Software Developer, Author,
Recording Engineer
AIM: Reboog711 | Phone: 1-203-379-0773
--
My Company: <http://www.dot-com-it.com>
My Podcast: <http://www.theflexshow.com>
My Blog: <http://www.jeffryhouser.com>

Reply via email to