Easily disposable context

combes.benjamin's Avatar


24 Feb, 2010 01:40 PM

Hi !

I would find it great to have some functions making context cleaning easier.
I am working on tests of modular applications, and would find it usefull to have functions (such as a myModuleContext.dispose() ) to clean a whole context when I unload the module corresponding to that context (unmaping commands, values, singletons, mediators...).
Such a function would be called once you made all manual cleanup (removing timers, non-framework event listners...), that would make it easier and avoid memory leaks if I avoid to remove someting.
Is that possible, too complicated, or am I just thinking wrong ?

  1. Support Staff 1 Posted by Shaun Smith on 25 Feb, 2010 10:17 AM

    Shaun Smith's Avatar


    The general approach is to dispatch a SHUTDOWN event on your modules's IEventDisptcher - that way, any interested parties can clean themselves up. When shutdown has completed, fire off a SHUTDOWN_COMPLETE event - that way your shell can determine that the module has been properly cleaned up.

    You can dispatch the initial SHUTDOWN event from the outside by dispatching it directly on the context itself (the context happens to be the IEventDispatcher for itself), so you can tell a module to shutdown from your shell.

    Does that make any sense?

  2. 2 Posted by combes.benjamin on 25 Feb, 2010 10:27 AM

    combes.benjamin's Avatar

    Helle Shaun !

    Thanks for your fast answer !
    In fact, I allready use these SHUDOWN and SHUTDOWN_COMPLETE events, but I was thinking of a context self disposing itself with a single function, without having to code it manually (like unmapping all commands that have been mapped, deleting and unmapping all actors, the same for mediators), made automatically by RobotLegs without having to write it mannualy, risking to forget things.
    So the context itself would be responsible to make the whole cleanup, and it would be without having to code anything. The only thing that would have to be coded are more manual things (disposing model contents for instance) independent from the framework.
    So when you would like to unload a module, you would still dispatch the SHUTDOWN event, each parties interested would cleanup itself but ONLY for non framework things (like a bitmapdata model would dispose the bitmap data for instance, but not unmap itself from robotlegs), and once this done, a "dispose" methode would be called on the context, cleaning all framework things like mappings.
    Is this something that makes sense or am I thinking wrong?
    Anyway, thanks again for your great job, I really love using Robotlegs daily !!!

  3. Support Staff 3 Posted by Shaun Smith on 25 Feb, 2010 11:02 AM

    Shaun Smith's Avatar

    If your objects have been designed well, you should be able to do this (wherever you constructed the context):

    context = null;

    And the Garbage Collector should take care of the rest. There is no need to explicitly unmap things - in terms of framework classes, everything has been designed with GC in mind. Bear in mind that GC will only run when the Flash Player actually needs more memory.

    What I'm getting at is that you should use SHUTDOWN to trigger any custom cleanup (usually pretty minimal), and then just drop the reference to the context. There are no statics or Singletons that will hold on to the Robotlegs objects.

    I recommend trying it out with the Flash Builder profiler:

    Create a Context as usual. Make it do all kinds of memory consuming things. Then just drop the reference to the context, and instantiate a new context (or a new memory consumptive object). Do this a couple of times and see what hangs around. If all goes well, nothing should survive.

  4. Stray closed this discussion on 13 Feb, 2011 07:07 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac