开发者

AS3: why is this happening?

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 to开发者_运维知识库uch 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜