tags:

views:

417

answers:

1

I have got a memory leak problem in the example below(u can download the code from the link)

http://brandonmeyer.net/projects/SuperPanelDemo/SuperPanelDemo.html

Running in Profiler:- What I am trying to do is creating new panels by selecting the Add new panel button. I am selecting option allow Close (check box).

(After creating few panels and closing these panels i could find there is memory leak with the SuperPanel class)

So my problem is how to resolve this memory leak. I tried by changing optional parameters in addEventListener, but that didn't work. Can someone provide me the solution

+1  A: 

I think your problem is related to your bindings. BindingUtils.bindProperty is being given an your panel instance, trying it to the object graph and making it inapplicable for garbage collection.

bindProperty returns a ChangeWatcher, which you can use to unregister the binding when you are done with it. Something like the following:

var watchers : Array = [];

var panel:SuperPanel = new SuperPanel();
panel.width = 300;
panel.height = 200;
panel.minWidth = 200;
panel.minHeight = 100;

panel.title = "My Panel " + (panelContainer.numChildren + 1);
panel.addEventListener(CloseEvent.CLOSE, function(event:CloseEvent):void{
    for each(var watcher : ChangeWatcher in watchers) {
        watcher.unwatch();
    }

    event.target.parent.removeChild(event.target);
});

watchers.push(BindingUtils.bindProperty(panel, "allowDrag", allowDragCheck, "selected"));
watchers.push(BindingUtils.bindProperty(panel, "allowResize", allowResizeCheck, "selected"));
watchers.push(BindingUtils.bindProperty(panel, "allowClose", allowCloseCheck, "selected"));
watchers.push(BindingUtils.bindProperty(panel, "allowMaximize", allowMaxCheck, "selected"));
watchers.push(BindingUtils.bindProperty(panel, "allowMinimize", allowMinCheck, "selected"));

panelContainer.addChild(panel);

Also, you have not overridden the clone event in SuperPanelEvent which will cause you issues later. See this question for more details.

Richard Szalay
Hey its great its working partially. That is when I am closing first oppup and running GC, its not removing that instance. It is removing from next.That is i am always having 1 extra instance that it is supposed to be.Can you tell me where to change
Fresher4Flex
This is a bug in (IIRC) the UIComponent class which retains references when it shouldn't. I'll try to find you an article that explains it, but at least your memory will be cleaned up (albeit slightly later than you wanted). Side note: you shouldn't call the GC yourself.
Richard Szalay
Hey Richard, Your solution really helped me a lot.I am trying for this solution from 2 days. Thank you very much.
Fresher4Flex
@Fresher4Flex - No worries. If you think my answer is what helped you solved your problem, you can mark it as "the answer" by clicking on the "tick" to the left of the question.
Richard Szalay