Views & Mediators life cycle
Hi there,
After searching on this knowledge base and through google, i
can't get my view killed properly.
I'm runngin profiler on Flash builder to see if the previous view
is killed but it persists on memory.
I've been using autoRemove for mediator's mapping, then i'm
switching from a view to another simply by removing and adding
views on the contextView :
`(contextView as
IVisualElementContainer).removeElement(myViewA);
(contextView as IVisualElementContainer).addElement(new ViewB());`
i'm removing all listeners by overriding the onRemove method in my mediator.
Flash Builder Profiler tells me that a reference on my view is still in place (so GC won't do the job), and the reference that is pointing is actually the addElement on my contextView ... even if i ve done a removeElement on this particular view ...
Don't know if i'm clear, but don't hesitate to ask for more informations if needed.
Thx in advanced
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Ondina D.F. on 25 Apr, 2012 03:48 PM
Hey Jim,
First question:
Have you tried to force the gc: “Run Garbage Collector” in the profiler?
After removing it from the display list an object doesn’t get gc-ed right away, it just becomes a candidate for gc.
Ondina
3 Posted by jimnastiq on 25 Apr, 2012 03:52 PM
Hi Ondina,
First, thx for your reply and yep, i've tried manually with profiler and with System.gc() method. but no result.
Support Staff 4 Posted by Ondina D.F. on 25 Apr, 2012 03:53 PM
Next questions:
What kind of a component is myViewA?
Does it have sub-components?
5 Posted by jimnastiq on 25 Apr, 2012 03:56 PM
It's a component developped by myself (a view with list => itemrenderers and some buttons, ...) .
I've already checked all of those sub views to see if listeners is still plugged ;)
Support Staff 6 Posted by Ondina D.F. on 25 Apr, 2012 04:10 PM
Have you tried to add/remove the components directly within the contextView (the display object) ?
Can you try to add/remove a simple view without sub-components the same way you did with your myViewA and see if it gets gc-ed?
Support Staff 7 Posted by Ondina D.F. on 25 Apr, 2012 04:12 PM
Are you sure it is the same instance of myViewA that you are trying to remove?
8 Posted by jimnastiq on 25 Apr, 2012 04:16 PM
I 'm sure i'm removing the same instance (same memory address).
I'll check right now with a simplier view. And i already remove the view directly within the contextView ( see my 1st post)
9 Posted by jimnastiq on 25 Apr, 2012 04:30 PM
I've checked the onRemove method on my mediator to see if my view is still in the contextView with (view is the injected view in the mediator) :
if ( contextView.contains(view) )and it returns false ...
Support Staff 10 Posted by Ondina D.F. on 25 Apr, 2012 04:35 PM
Is it possible to attach your app? I can take a look at it.
(Make the discussion private until I download the file)
Support Staff 11 Posted by Ondina D.F. on 26 Apr, 2012 06:41 AM
In the snippet below, if you don’t set someView to null, it won’t get gc-ed. The reference to SomeView will keep it alive.
private var someView:SomeView;
protected function addViewsHandler():void
{
someView=new SomeView();
this.addElement(someView);
}
protected function removeViewsHandler():void
{
this.removeElement(someView);
someView =null;
}
Let me know if it solves your problem.
12 Posted by jimnastiq on 26 Apr, 2012 07:25 AM
Thx again for your help Ondina!
I will try to make you a project illustrating the problem.
And for your last post, as i tell you i'm not keeping reference of the new instance of my view i just create it in the addElement method :
addElement(new MyView())
and to remove it i just kept a reference on it somewhere else, and this reference is "cleaned" when i change the view 'cause this properties take the new view instance.
keep you informed when i post the project ;)
Support Staff 13 Posted by Ondina D.F. on 26 Apr, 2012 07:49 AM
Sure, no problem:)
Yeah, it’s better to post an example, so I can see how you’re handling this reference to the view.
14 Posted by jimnastiq on 26 Apr, 2012 08:20 AM
Ondina,
you can find attached the mini-project illustrating the problem and a screenshot of profiler showing the occurence of ViewOne.
Support Staff 15 Posted by Ondina D.F. on 26 Apr, 2012 08:26 AM
Cool. Downloaded the files. I'll let you know about my findings as soon as possible.
Support Staff 16 Posted by Ondina D.F. on 26 Apr, 2012 09:22 AM
I think, I found the culprit. I'll be back soon with explanations
17 Posted by jimnastiq on 26 Apr, 2012 09:23 AM
yeah ! you rocks :)
Support Staff 18 Posted by Ondina D.F. on 26 Apr, 2012 10:34 AM
I changed the event listener in ViewOneMediator from
eventMap.mapListener(view.myLabel, MouseEvent.CLICK, onMouseClick, MouseEvent);
to
eventMap.mapListener(view, ViewsEvent.ViewOneOnClick , onViewClick);//a custom event – the name is bad, I know
but that didn’t solve the problem.
Then I commented out the button and I added an event listener to the Label’s click.
viewModel.currentView=null;
and after that ViewOne got gc-ed!
I tried to set the view to null in onRemove() as well, but without the 2 other solutions mentioned above it didn’t work.
Then I added the same logic to ViewTwo and ViewTwoMediator.
In this scenario viewModel.currentView was replacing the views accordingly, and indeed, it didn’t keep a reference to the previous view.
Thus, the viewModel.currentView=null; was superfluous.
So the conclusion is, that the Button is the culprit. Bad, bad Flex Buttons;) I don’t have time to investigate why it is behaving like this and whether it is indeed a bug or not. If you find out what’s wrong, please let us know.
19 Posted by jimnastiq on 26 Apr, 2012 10:41 AM
Thx Ondina,
could you share your modified project please ? 'cause i've made changed you mentioned (eventmap on label) but the view is still not gc-ed ...
Support Staff 20 Posted by Ondina D.F. on 26 Apr, 2012 10:51 AM
Oh, ok. I’ll have to clean it up first, because it’s a mess right now, after the many things I tried.
On a side note, why aren’t you using a ViewNavigator or something like this, since it’s a mobile app?
Support Staff 21 Posted by Ondina D.F. on 26 Apr, 2012 11:16 AM
ViewOneMediator
I attached the fxp as well. Note: I had to change the Main-app.xml to air 3.2. You’ll have to change it back to 3.1 as it was in your project.
Support Staff 22 Posted by Ondina D.F. on 26 Apr, 2012 11:23 AM
Oops, I thought I removed all the files. I’m attaching the clean one again.
Support Staff 23 Posted by Ondina D.F. on 26 Apr, 2012 12:19 PM
I can confirm that setting the focusEnabled to false makes it work with the button too.
Button id="btnView" label="Go To View 2 !!!" focusEnabled="false"
(focusEnabled is true by default)
24 Posted by jimnastiq on 26 Apr, 2012 12:24 PM
Holly crap !
I confirm what you said with focusEnabled set to false it's done !
Now i'll have to check on all my component on my custom view in my big project :)
Thx again for your time and your help Ondina!
jimnastiq closed this discussion on 26 Apr, 2012 12:24 PM.