8 Replies Latest reply on May 22, 2009 8:03 AM by run,ryan!

    event listener question

    ice9_us Level 1

      AssignedNotOwnedBox.addChild(notMyTickets);

      AssignedNotOwnedBox.addEventListener(MouseEvent.CLICK,_root.selectTicket,

      false, 0, true);

       

      if i add childeren this way.. and i have say.. 5 or 6..

      I remove all the children by calling removeAllchildren();

      but

      how do i remove the eventListener when calling removeAllchildren();

       

      do the listeners remove with the removeallchildren();  call ?

        • 1. Re: event listener question
          run,ryan!
          removeChild()method

          public override function removeChild(child:DisplayObject):DisplayObject

                Removes a child DisplayObject from the child list of this Container.    The removed child will have its parent    property set to null.    The child will still exist unless explicitly destroyed.    If you add it to another container,    it will retain its last known state.           

           

          If you no longer need an event listener, remove it by calling removeEventListener(), or memory problems could result. Objects with registered event listeners are not automatically removed from memory because the garbage collector does not remove objects that still have references.

           

          A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.  

          so you have to remove eventListener manually after you removeAllChildren()

          • 2. Re: event listener question
            BennyBeta Level 4

            Because you passed true for the last argument to addEventListener(), the listener was added as a weak reference.  This means you shouldn't have to manually remove it.

             

            Ben Edwards

            • 3. Re: event listener question
              ice9_us Level 1

              I know that... my question is...

               

               

              if i add via:

               

               

              for ( var a:Number=0; somevalue[a]; a++ ) {

              notMyTickets =

              false, 0, true);

              new TicketDisplayBox();

              AssignedNotOwnedBox.addChild(notMyTickets);

              AssignedNotOwnedBox.addEventListener(MouseEvent.CLICK,_root.selectTicket,

               

               

              }

               

               

               

              i add five children..

              I want  to remove them ALL..  Do i ...

               

              for (  a=0;AssignedNotOwnedBox.removeEventListener....; a++) {

               

              AssignedNotOwnedBox.removeEventListener(.......

              ....removeChildat(index);

               

              }

               

               

               

               

               

              how do i make sure the listener and child are moved for that specfic instance...????

               

              • 4. Re: event listener question
                ice9_us Level 1

                ok.. so my thinking is correct..

                removeAllchildren() is safe and will not be a cause of any memory leak when i remove the children right?

                • 5. Re: event listener question
                  BennyBeta Level 4

                  Yes, your thinking is correct.

                  • 6. Re: event listener question
                    ice9_us Level 1

                    thanks for your time.. i hope to be able to repay some how one day

                    • 7. Re: event listener question
                      BennyBeta Level 4

                      You're welcome.  Thanks for saying thanks!

                      • 8. Re: event listener question
                        run,ryan! Level 3

                        well... I don't think so...

                        gc is not always runing, so it may not removed immediately.

                        and after removeAllChildren, it will gc the weak listener first, any change during this time will trigger the listener (it may changed programmatically, though you are likely not to do that). And after that, it will collect removed children when gc is called the second time (its not collected the first time 'cause you have ref of your removed children on listener)