views:

27

answers:

1

I have the following problem with objects in actionscript3:

var o:Object = new Object();
destroyObject(o);
trace(o); // [object Object]

function destroyObject(obj:Object):void{
   obj = null; 
   trace(obj); // null
}

Since objects are passed by reference in AS3 I assume that the previous code would change o to null, but it doesn't. Could someone explain me 'why'?

Thanks.

+2  A: 

Objects are passed by reference, but the reference itself is passed by value. The statement obj = null sets the parameter obj to null, which means that obj no longer references the object, but the object itself is still present, and is still referenced by the variable o when destroyObject returns.

Marcelo Cantos
thanks, so, in other words, I can't make it work inside the function, it has to be outside?
negative
correct. the object will be released when all references to it are removed, so you'd have to remove the "o" reference first.
UltimateBrent
I think it is called "passed by value", same as in Java.
negative
@negative. You're right. Though ,sadly, the actionscript documentation insisst on saying that actionscript is pass-by-ref, so this confuses a lot of people.
Juan Pablo Califano