views:

29

answers:

2

Hi, I just encounter a strange problem:

var a:ClassA = new ClassA;
var b:ClassA = a;

The program keeps running sometime, the a = null, b = null.

The program is a complex one, I am sure that no part will touch a, and b. My question is, will the runtime(garbage collector) to collect the memory of "a" and then assign a and b to null?

I am confused, thanks!

A: 

try

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

The () at the end of the class name calls the constructor so that you actually have your ClassA.

Robusto
Thanks for the answer, but it's not the right answer...
Bin Chen
+1  A: 

The garbage collector will reclaim the memory that this instance of ClassA occupies, only once there is no longer a reference to it. As long as a OR b references that memory location, the instance will remain. If these are local variables, then the instance will be picked up by the GC at some point after the function/method exits. If those are instance variables then they will remain until after the defining class' instance is collected.

echo