7 Replies Latest reply on Feb 3, 2010 11:44 AM by Francisc

    Removing Event Listeners

    Francisc Level 3



      I have a PopUpManager window that fires an event just before closing (not the CloseEvent).


      I am listening for that event and handling it in a function.


      Q: Is it best practice to remove the listener in the function that handles it or calling PopUpManager.removePopUp on that popup automatically does the "cleaning" as well?


      Thank you!

        • 1. Re: Removing Event Listeners
          archemedia Level 4

          where and how are you adding the event listener?



          • 2. Re: Removing Event Listeners
            Francisc Level 3

            The listener gets added right after the PopUpManager.addPopUp is called.

            The handling function is inside the parent of the pop up window, the window just has a form and fires an even after data is sent.

            • 3. Re: Removing Event Listeners
              archemedia Level 4

              Removing the eventListeners on a popup window is necessary I believe.


              In your event handling function, you can remove the event listener by event.target.removeEventListener(..., ...). It doesn't matter that your component is handle by the popUpManager, you'll still have a reference to it.




              In main app:


              var win:myClass = PopUpManager.createPopUp(this, myClass, true) as myClass;

              win.addEventListener("myEvent", myHandler);


              private function myHandler(evt:Event):void


                   evt.target.removeEventListener("myEvent", myHandler);



              In custom myClass:


              private function handleSomething(evt:Event):void
                              var ev:Event = new Event("myEvent");
                              if (isPopUp)
                                  PopUpManager.removePopUp(this as IFlexDisplayObject);


              Does this help?



              1 person found this helpful
              • 4. Re: Removing Event Listeners
                Francisc Level 3

                I know how to do it, I was just wondering if I should do it or if the removePopUp will leave it to the Garbage Collector...

                So I take it I am responsbile for cleaning the `mess` up?

                • 5. Re: Removing Event Listeners
                  archemedia Level 4

                  All the articles I found on this subject warn to remove the event listeners, so I think they're right.

                  On the other hand, I couldn't find an adobe article stating this.

                  So, until further notice, I guess it's safest to remove them. I found out that I forgot to do that in one of my projects too



                  • 6. Re: Removing Event Listeners
                    Flex harUI Adobe Employee

                    A listener simply creates a reference from the dispatcher to the listener.

                    In the code below, there is now a reference from "win" to the class that

                    owns "myHandler" which is the document, so adding that listener will not

                    cause a leak.

                    • 7. Re: Removing Event Listeners
                      Francisc Level 3

                      There is no code below, but I understand what you are saying! Thanks!