Home > Flash development, Flex development > RobotLegs, mediators and memory leaks

RobotLegs, mediators and memory leaks

An important lesson I learned today: the onRegister() method of a Mediator in the RobotLegs framework get’s fired when it’s view is added to the stage. This means that the event mappings and event listeners that you place in the onRegister() method will get invoked each time and (assuming you are adding and removing the view multiple times) unless you use the onRemove() method to un-map and remove the event listeners, you will most certainly get a memory leak. Good to know!

This is a little inconvenient because I was hoping that I could use the mediator event handlers to update a view while it is not on the display list. As it turns out, the mediator will not respond to event mappings when it’s view is removed from the stage. Also good to know.

Any input on this appreciated!

Categories: Flash development, Flex development Tags:
  1. Anonymous
    May 5th, 2010 at 12:32 | #1

    Another fun thing about the interaction of robotlegs mediators and states. Its possible for the state change to cause robot legs to create a mediator through the mediator map. This is what you discovered. Whats annoying is that under certain conditions the onRemove can get fired BEFORE the onRegister does. The onRegister function is fired by mapping an event listener to the creation complete event. If the application for some reason starts the mediator then quickly decides to change state again then it will fire that onRemove before your component thats tied to the mediator has a chance to fire creation complete. Drop into the the base class for the mediator map and you can see that the event handler is not removed in the onRemove function…but only once it has fired in the creation complete handler. wheeee!

  2. May 5th, 2010 at 13:16 | #2

    With auto-mediation (the default when mapping a mediator) mediators are created and removed automatically as view components arrive on and leave the stage. The mediator is supplied with an EventMap that you can use to wire up your listeners, and when the mediator is removed it’s listeners will be removed along with it. Also, you don’t have to use auto-mediation, you can create, register and remove mediators manually too:

    http://knowledge.robotlegs.org/discussions/questions/117-any-other-way-to-create-mediators-other-than-listening-to-added_to_stage#comment_1363130

  3. May 5th, 2010 at 13:51 | #3

    Dear Anonymous,

    Robotlegs is Open Source: you could have filed a bug report or even fixed the code yourself, but you chose to moan.. thanks for the feedback.

  4. Matan Uberstein
    May 5th, 2010 at 14:19 | #4

    Nice one Shaun!

  5. May 5th, 2010 at 14:58 | #5

    @shaun Thanks for the link and your twitter response. In my case I just modified my mediator mapping like this:

    mediatorMap.mapView(Map, MapMediator, Map, true, false);

    so that the creation is automatic but the deletion isn’t. Just a coincidence that the view is a map … as in Yahoo map :-0

  1. No trackbacks yet.