>If you add a normal event listener to a child,
>and then remove that child, the child is _not_ garbage collected.
>Objects are only garbage collected when all references to the object
>are removed. Adding a normal event listener causes a reference to be
>created invisibly within the event system.


I think this is where we disagree. If a parent adds a listener to a child,
this means the child has a reference to the parent, not the other way
around. What you call the event system can be thought of as an array
with handler methods on the child instance. This means that if you add
a normal event listener to a child and then remove it, it will be
garbage collected. There is no such thing as "a
reference to be created invisibly within the event system".

>You can't use weak references on an anonymous event listener.

In my last email I agreed with you on this :)

>You must remove all normal listeners from an object before it will be
>garbage collected.

Again, I do not think this is correct. An object will be garbage collected
if there are no references to it. Adding listeners to an object will not
create references to the object itself.


An example (not usefull, but to illustrate the point):

package
{
   class TestClass
   {
      public function TestClass()
      {
         var o:EventDispatcher = new EventDispatcher();
         o.addEventListener(Event.COMPLETE, _handler);
      }; // At this point o will be marked for
garbage collection, no references to o left.

      private function _handler(e:Event):void
      {
      };
   };
};


Another example (again not usefull, but to illustrate the point):

package
{
   class TestHandler
   {
      public function TestHandler()
      {
      };

      public function handler(e:Event):void
      {
      };
   };
};

package
{
   import flash.display.Sprite;

   class TestClass extends Sprite
   {
      public function TestClass()
      {
         var o:TestHandler = new TestHandler();
         stage.addEventListener(Event.RESIZE, o.handler);

      }; // At this point o will not be marked for garbage collection, stage
will still have a reference to it.
   };
};


I hope you agree on this. If not I am open to convincing arguments :)



Greetz Erik


On 3/13/08, Cory Petosky <[EMAIL PROTECTED]> wrote:
>
> >  The common use for weak event
> >  listeners is when adding listeners to the stage
>
>
> I disagree. Weak event listeners should be used in nearly all cases.
>
>
> >  In most cases there is actually no real benefit of creating weak event
> >  listeners. In most cases parents add
> >  listeners to their children. Children are removed and that's that.
>
>
> This is not correct. If you add a normal event listener to a child,
> and then remove that child, the child is _not_ garbage collected.
> Objects are only garbage collected when all references to the object
> are removed. Adding a normal event listener causes a reference to be
> created invisibly within the event system.
>
> You must remove all normal listeners from an object before it will be
> garbage collected.
>
>
> > Hmmm, if it was garbage collected, why would I remove the listener, isnt
> >  that the point of weak references?
>
>
> You can't use weak references on an anonymous event listener. The only
> reference to that function is in the event system itself, so if you
> use a weak reference, the garbage collector considers the anonymous
> function eligible for garbage collection. Thus, the garbage collector
> removes your event listener immediately, and you never get your event.
>
> (When I say "immediately," I really mean "at a random time when the GC
> is invoked." Sometimes that's immediate, sometimes its not, but you
> should assume immediacy so your code doesn't randomly break.)
>
>
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to