在AS3中,从阶段中删除子级是否会删除对该子级的所有引用,以便可以由垃圾收集器清除该子级? [英] In AS3, does removing a child from stage remove all references to it so that it can be cleaned up by the garbage collector?

查看:84
本文介绍了在AS3中,从阶段中删除子级是否会删除对该子级的所有引用,以便可以由垃圾收集器清除该子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个AIR应用程序,该应用程序具有一个主类,该主类创建许多动画片段的实例并将其添加到容器中.在整个应用程序使用过程中,这些剪辑经常被销毁并创建新的剪辑.

I'm creating an AIR app that has a main class that creates lots of instances of a movieclip and adds them to a container. Throughout the use of the app, these clips are frequently destroyed and new ones created.

当我创建这些影片剪辑时,我的主类向它们添加了事件侦听器.电影剪辑内部还具有事件侦听器.

When I create these movieclips, my main class adds event listeners to them. The movieclips also have event listeners within themselves.

每当我更新显示内容时,我都会使用以下功能从其容器中删除所有这些动画片段:

I use the following function to remove all of these movieclips from their container whenever I'm updating the display:

        for (var i = 0; i < this.mainContainer.numChildren; i++) 
        {
            mainContainer.removeChild(mainContainer.getChildAt(i));
            //mainContainer.getChildAt(i)=null;
        }

我想知道这是否足以为垃圾回收准备那些实例,即是否杀死了我的主类添加的事件侦听器?还是我需要先删除事件监听器,然后再删除每个孩子?

I'm wondering if this is sufficient to prepare those instances for garbage collection ie does that kill the event listeners added by my main class? Or do I need to go through and remove the event listeners first before removing each child?

此外,将每个实例的null放在哪里适合呢?上面注释掉的行给出了错误:

Also, where does nulling each instance fit into this? The above commented out line gives the error:

1105: Target of assignment must be a reference value.

谢谢

推荐答案

删除所有子级

您的脚本不会删除 mainContainer 的所有子项,而是仅删除一半.为什么?因为每次您删除一个孩子,其余的都会关闭以吞噬释放的索引,因此您的脚本会按以下方式删除它们:

Your script won't remove all children of mainContainer but rather the half only. Why? Because each time you remove a child, the rest closes to eat up the freed index, so your script removes them as following:

Initial picture  :  0 1 2 3 4 5 6 7 8 9
Remove child at 0:  1 2 3 4 5 6 7 8 9
Remove child at 1:  1 3 4 5 6 7 8 9
Remove child at 2:  1 3 5 6 7 8 9
Remove child at 3:  1 3 5 7 8 9

从给定容器中删除 ALL 子项的正确方法是向后循环,该循环会在给定时刻删除最后一个子项:

The correct way to remove ALL the children from a given container is either a backward loop that removes at the given moment the last child:

for (var i = mainContainer.numChildren - 1; i >= 0; i--) 
{
    mainContainer.removeChildAt(i);
}

while 循环,该循环将在深度为0时移除子项,同时存在以下任何情况:

Or a while loop that removes children at depth 0 while there are any:

while (mainContainer.numChildren > 0)
{
    mainContainer.removeChildAt(0);
}

但是,有一种更简单的方法来清空容器(可从Flash Player 11开始使用):

However, there's a much simpler way to empty a container (available from Flash Player 11 and on):

mainContainer.removeChildren();

垃圾收集器

对于GC来说,一般的想法是,应用程序范围(附加到阶段的对象和静态类成员)没有对这些对象的有效引用.

What for GC, the general idea is that there's no valid references from the application scope (things attached to the stage and static class members) to these objects.

如果您确定范围内没有任何对象引用或订阅它们,则GC将正确执行其工作.

If you are sure that nothing on the scope refer them or is subscribed to them, then GC will do its job properly.

我个人总是编写一个称为 destroy(...)的方法,该方法可拆解给定对象中的所有内容:取消订阅所有事件侦听器,将 Array 呈现为长度为0,为每个 Object 类型的变量分配 null ,删除子级,等等.

Personally I always compose a method called destroy(...) that dismantles everything within the given object: unsubscribes all event listeners, renders Arrays to a length of 0, assigns null to every and each Object-typed variable, removes children, etc.

我也同意 Jyreel ,依靠显示容器为您保存东西不是程序员做事的方式,而是使用这种方式或设计更复杂的数据结构来做出决定取决于您和您的理解.

I also agree with Jyreel, relying on display containers to hold things for you is not a programmer's way to do things, but to use this way, or to devise more complicated data structures, that decision is up to you and your understanding.

这篇关于在AS3中,从阶段中删除子级是否会删除对该子级的所有引用,以便可以由垃圾收集器清除该子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆