9 Replies Latest reply on Aug 5, 2008 10:34 AM by jonesf

    Disturbing: event listeners still firing after objects have been removed

    jonesf Level 1
      So I keep reading how dangerous event listeners are in regards to memory leaks. Documents and blogs repeatedly say that you must clean up your event listeners are they will prevent your objects from being garbage collected. But what I haven't really seen are any good standard methods to do this. Such as... in which 'destruct' type events do people typically do this. And how do they keep track of all their event listeners so they know which ones to clean up at the end... etc. This just seems extremely error prone.

      Anyway... back to my immediate concern...

      I have an MXML that starts out with something like this...
      <mx:Module
      xmlns:mx=" http://www.adobe.com/2006/mxml"
      creationComplete="init(event)"
      xmlns:local="*"
      deactivate="{trace('deactivating ' + this)}"
      >

      Notice the deactivate listener. This event seems to trigger whenever the browser becomes unfocused. If I'm in the flex app, then click on another non-browser window, I can see the trace output of these objects 'deactivating'. Ok, so these MXML objects are created in the Flex app on the fly in actionscript and added to a vbox. They are frequently created, added to vbox, then removed from the vbox.
      THE PROBLEM is that even after they're removed from the vbox, AND I HAVE NO OTHER KNOWN REFERENCES to them, the deactivate events will still fire from these removed objects. In other words, I have these phantom objects still hanging out there firing events. So my assumption is that they will never by GC'd.

      Can anybody offer me any guidance? This is scary stuff.

      Thanks
        • 1. Re: Disturbing: event listeners still firing after objects have been removed
          Level 7

          "jonesf" <webforumsuser@macromedia.com> wrote in message
          news:g6vdbm$609$1@forums.macromedia.com...
          > So I keep reading how dangerous event listeners are in regards to memory
          > leaks.
          > Documents and blogs repeatedly say that you must clean up your event
          > listeners
          > are they will prevent your objects from being garbage collected. But what
          > I
          > haven't really seen are any good standard methods to do this. Such as...
          > in
          > which 'destruct' type events do people typically do this. And how do they
          > keep
          > track of all their event listeners so they know which ones to clean up at
          > the
          > end... etc. This just seems extremely error prone.
          >
          > Anyway... back to my immediate concern...
          >
          > I have an MXML that starts out with something like this...
          > <mx:Module
          > xmlns:mx=" http://www.adobe.com/2006/mxml"
          > creationComplete="init(event)"
          > xmlns:local="*"
          > deactivate="{trace('deactivating ' + this)}"
          > >
          >
          > Notice the deactivate listener. This event seems to trigger whenever the
          > browser becomes unfocused. If I'm in the flex app, then click on another
          > non-browser window, I can see the trace output of these objects
          > 'deactivating'.
          > Ok, so these MXML objects are created in the Flex app on the fly in
          > actionscript and added to a vbox. They are frequently created, added to
          > vbox,
          > then removed from the vbox.
          > THE PROBLEM is that even after they're removed from the vbox, AND I HAVE
          > NO
          > OTHER KNOWN REFERENCES to them, the deactivate events will still fire from
          > these removed objects. In other words, I have these phantom objects still
          > hanging out there firing events. So my assumption is that they will never
          > by
          > GC'd.

          Did you try using weak references? Also, make sure you don't add any event
          listeners or use bindings in MXML, as these can't be cleaned up.


          • 2. Re: Disturbing: event listeners still firing after objects have been removed
            cheftimo Level 2
            What you are describing is normal behavior.

            When you remove an Object, you must first unregister its listener(s) (removeEventListener()), or it will be "stranded" – it will remain in that Object's listener list.

            Carlos
            • 3. Re: Disturbing: event listeners still firing after objects have been removed
              jonesf Level 1
              Thanks. What you guys are saying, jives with things that I have read. But it is just seeming impossible to implement and still maintain a decent level of productivity.
              But let's say I go for it and I take out all event handlers definitions from all of my MXML tags (ugh), how would I get them in done in Actionscript since I need to setup initial event listeners in my MXML so that my actionscript gets called? Example:

              <mx:Module
              xmlns:mx=" http://www.adobe.com/2006/mxml"
              creationComplete="init(event)"
              >

              private function init(event:Event)
              {
              obj1.addEventListener(xxxx);
              obj2.addEventListener(xxxx);
              }


              The creationComplete event listener was defined in MXML. Are you saying that since I did that, this object will never be GC'd? So then what can I do?

              • 4. Re: Disturbing: event listeners still firing after objects have been removed
                Gregory Lafrance Level 6
                I would think the removeEventListener code should be in the code that removes the objects.
                • 5. Re: Disturbing: event listeners still firing after objects have been removed
                  Level 7

                  "jonesf" <webforumsuser@macromedia.com> wrote in message
                  news:g6vgmn$9gc$1@forums.macromedia.com...
                  > Thanks. What you guys are saying, jives with things that I have read.
                  > But it
                  > is just seeming impossible to implement and still maintain a decent level
                  > of
                  > productivity.
                  > But let's say I go for it and I take out all event handlers definitions
                  > from
                  > all of my MXML tags (ugh), how would I get them in done in Actionscript
                  > since I
                  > need to setup initial event listeners in my MXML so that my actionscript
                  > gets
                  > called? Example:
                  >
                  > <mx:Module
                  > xmlns:mx=" http://www.adobe.com/2006/mxml"
                  > creationComplete="init(event)"
                  > >
                  >
                  > private function init(event:Event)
                  > {
                  > obj1.addEventListener(xxxx);
                  > obj2.addEventListener(xxxx);
                  > }
                  >
                  >
                  > The creationComplete event listener was defined in MXML. Are you saying
                  > that
                  > since I did that, this object will never be GC'd? So then what can I do?

                  No, don't think that's right, since the object is referencing itself. I
                  don't think an interior reference will prevent gc.

                  HTH;

                  Amy


                  • 6. Re: Disturbing: event listeners still firing after objects have been removed
                    jylaxx Level 1
                    You can find a lot of articles about Flex GC and memory leaks in Flex/AIR. That's certainly a big problem and I hope Adobe is highly concern by that.
                    http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html
                    http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3 -with-air/
                    • 7. Re: Disturbing: event listeners still firing after objects have been removed
                      Gregory Lafrance Level 6
                      I noticed you add event listeners like this:

                      <mx:Module
                      xmlns:mx=" http://www.adobe.com/2006/mxml"
                      creationComplete="init(event)">

                      private function init(event:Event){
                      obj1.addEventListener(xxxx);
                      obj2.addEventListener(xxxx);
                      }

                      I'm wondering if it is possible to add these event listeners in the module constructor, and then have a method called in the deactivate listener to remove the listeners added in the constructors.

                      Also, consider loading these modules in AS, not MXML. MXML is great for many purposes, but maybe AS is better in this case. It seems strange to cringe from AS. That's where the real power is anyway.

                      I understand exists MXML to a certain extent to help persons less comfortable with programming (I'm not saying you are one of them, or that this is a bad thing) be more involved in developing Flex apps. Thermo will take this one step further. But the future is really blurring the lines between designer and programmer, and the big opportunities are for those who master both. At least, this is what I believe...
                      • 8. Re: Disturbing: event listeners still firing after objects have been removed
                        Level 7

                        "Greg Lafrance" <glafrance@chikaradev.com> wrote in message
                        news:g71mf9$k56$1@forums.macromedia.com...
                        >I noticed you add event listeners like this:
                        >
                        > <mx:Module
                        > xmlns:mx=" http://www.adobe.com/2006/mxml"
                        > creationComplete="init(event)">
                        >
                        > private function init(event:Event){
                        > obj1.addEventListener(xxxx);
                        > obj2.addEventListener(xxxx);
                        > }
                        >
                        > I'm wondering if it is possible to add these event listeners in the module
                        > constructor, and then have a method called in the deactivate listener to
                        > remove
                        > the listeners added in the constructors.
                        >
                        > Also, consider loading these modules in AS, not MXML. MXML is great for
                        > many
                        > purposes, but maybe AS is better in this case. It seems strange to cringe
                        > from
                        > AS. That's where the real power is anyway.
                        >
                        > I understand exists MXML to a certain extent to help persons less
                        > comfortable
                        > with programming (I'm not saying you are one of them, or that this is a
                        > bad
                        > thing) be more involved in developing Flex apps. Thermo will take this one
                        > step
                        > further. But the future is really blurring the lines between designer and
                        > programmer, and the big opportunities are for those who master both. At
                        > least,
                        > this is what I believe...

                        The past did a pretty good job of it as well. Nearly everyone I kept in
                        touch with who graduated from my university with a Fine Arts degree either
                        went back there to teach or became a programmer of some type. :-)


                        • 9. Re: Disturbing: event listeners still firing after objects have been removed
                          jonesf Level 1
                          Thanks for the suggestions everyone. I've been using MXML because I'm new to Flex and that's the path that the documentation and tutorials put me down. Although, the more I learn, the more problematic MXML seems to be. So, I'm starting to learn more about doing everything in AS.
                          I've spent the last few days with my head buried in the profiler trying to diagnose memory leaks. A pretty frustrating exercise so far. I'll post back here if I get any interesting results.