开发者

Is removeChild enough to completely remove a movieclip from Flash Player memory?

开发者 https://www.devze.com 2023-03-22 20:24 出处:网络
Will this line clip.removeChild(clip.getChildAt(0)); completely remove the child of clip at 0 index? I read somewhere you should set to null to all the references to that clip, but I have no other

Will this line

clip.removeChild(clip.getChildAt(0));

completely remove the child of clip at 0 index? I read somewhere you should set to null to all the references to that clip, but I have no other reference in my code. The clip at 0 was added via a regular addChild()开发者_开发技巧.


For the garbage collector to swipe your object you should:

-not have any other reference to the object throughout your code
-the object shouldn't be part of any collection (like Array or Vector)
-the current reference should be set to null

Be sure to pay extra attention to the second condition, the most common situation when the object is part of a collection you can't control directly is when it had a listener attached to it and when is part of the display list. On top of that, there are other situations when the object is part of a collection you can control, don't forget to remove it form there too.

Also, to force the garbage collector to swipe your object (only for testing, not production), you can use System.gc() and then check the memory with System.privateMemory


if you're removing them in a loop, do it like this:

while (clip.childNum > 0)
{
   var child:MovieClip = clip.getChildAt(0);
   clip.removeChild(child);

   // remove all listeners
   child.removeEventListener(...);

   child = null;
}

if "child" is a custom class you may call a kill() method to clean everything up inside your class/instance.


Not sure, if you still have reference on the clip, garbage collector may distroy the object, try to remove an event listener and force the clip reference to null.


If you have no references, no listeners or any other handle to the clip then it will eventually be garbage collected. Due to the way the GC works it might not immediately be removed from memory. Your DisplayObject will however immediately be removed from the display list.

But if you do something like this in one of your classes:

private var mc:MovieClip = new MovieClip();

private function addClip() : void {
    mc.addEventListener(Event.ENTER_FRAME, myListener);
    myClass.addChild(mc);
}

Then you'll want to properly remove mc like this:

private function removeClip() : void {
    mc.removeEventListener(Event.ENTER_FRAME, myListener);
    myClass.removeChild(mc);
    mc = null;
}
0

精彩评论

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