tags:

views:

577

answers:

2

I have problem on deleting component which is created on runtime. Please help me.

heres my code in creating component

var oh: ObjectHandles = new ObjectHandles;              
    oh.x =  event.localX-xOff;
    oh.y = event.localY-yOff;
    Canvas(event.target).addChild(oh);

    oh.addEventListener(KeyboardEvent.KEY_DOWN,deleteSel);
    oh.width=270;
    oh.height=200;
    oh.mouseChildren = true; 
    var vdo:FXVideo = new FXVideo;
    vdo.source = "http://thehq.tv/wp-content/uploads/flv/funny-people-trailer.flv";                 
    vdo.percentHeight = 100;
    vdo.percentWidth = 100;
    oh.addChild(vdo);

code in keyboard delete event

     private function deleteSel(event:KeyboardEvent):void
     {
            if(event.charCode == 127) 
            {
               FXVideo(ObjectHandles(event.target).getChildAt(0)).stop();
               delete(ObjectHandles(event.target).getChildAt(0));

               ObjectHandles(event.target).removeAllChildren();
               ObjectHandles(event.target).parent.removeChild(ObjectHandles(event.target));
               delete ObjectHandles(event.target);                
             }
      }

after I delete the Object Handles Component(inside is FxVideo Component) the memory usage is still there. How to remove the memory allocation of component after deletion?

+1  A: 

You need to remove the event listener, or you can add the event listener with a weak reference:

oh.addEventListener(KeyboardEvent.KEY_DOWN,deleteSel,false,0,true)

I would not recommend calling delete. Calling removeAllChildren should take care of it. Although, by looking at your code, that probably is not necessary either. Once you remove the event listener it should get cleaned up.

Osman
hello thank you for your response. By the way I do what you teach but unluckily it doesn't work. The memory allocation of component that is deleted is still there. I use IE and Mozilla to compare if the problem is in browser but the result is the same. Please help. Any other technique?thanks
Jejad
Garbage collection will not run immediately. Try running garbage collection from the profiler to see if it gets cleaned up.
Osman
I also try this: after ObjectHandles(event.target).parent.removeChild(ObjectHandles(event.target));I call System.gc() but still nothings happen
Jejad
A: 

delete only works with dynamic objects and won't have an affect here. I personally recommend explicitly removing the event listener:

event.target.removeEventListener(KeyboardEvent.KEY_DOWN,deleteSel);

as well as using the weak reference suggested by Osman.

Joel Hooks