2 Replies Latest reply on Dec 7, 2009 10:36 AM by cheftimo

    RE unregistering an event listener so it can be garbage-collected

    cheftimo Level 2

      One good practice in AS 3 is: When you register and event listener with an object, be sure your program also eventually unregisters that listener.

       

      So, let's say you created an <mx:RemoteObject> and the result listener did its job; you now have the data in an ArrayCollection or something, so you don't need the listener any longer.

       

      Question: how do you now unregister that result listener?

       

      Carlos

        • 1. Re: RE unregistering an event listener so it can be garbage-collected
          msakrejda Level 4

          That good practice only really applies to long-lived objects with short-lived listeners. Typically, when you are declaring a listener in MXML, it will be a method of your MXML component, so it will not be garbage collected anyway until your component is garbage collected. Normally, this is fine--there is no need to micro-manage the garbage collector unless your listener needs to allocate massive amounts of memory for whatever reason.

           

          If you really want to do this anyway, you can register the listener as an anonymous function and then call something like

           

          idOfRemoteObject.removeEventListener(ResultEvent.RESULT, arguments.callee);

           

          in the listener, but I think this is overkill. When your app is done with the component and the component is elligible for garbage collection, the listener will go with it.

          • 2. Re: RE unregistering an event listener so it can be garbage-collected
            cheftimo Level 2

            OK, this is what I suspected. I agree that micro-managing the garbage collection is not that important for what I do now. It's just that I am trying to really study AS, so I want to learn all I can.

             

            Thank you very much for your quick and instructive reply.

             

            Carlos