Hey there,

I was totally shocked this morning. If what you were saying was true it
would mean that if you created a class with a listener to itself and
instantiated it, it would never be garbage collected.

So I did some extra tests.

1. Your example with a weak listener (same result)
2. Trying to force garbage collection using System.gc and the
localconnection 'hack' (same result)

So I turned to the Flex Profiler. I used the following class to test things:

   import flash.display.Sprite;
   import flash.events.Event;
   import flash.system.System;
   import flash.text.TextField;

   public class testAS extends Sprite
      private var _t:TextField;
      private var _c:uint;
      private var _a:Array;

      public function testAS()
         _a = new Array();

         _t = new TextField();

         var a:SpriteClass = new SpriteClass();
         a.addEventListener(Event.ENTER_FRAME, _handler);

      private function _handler(e:Event):void
         _t.text = (_c++).toString();
         _a.push(new Counter());

Initialy the profiler showed the behaviour you were describing. However, if
I pressed
the "Run Garbage Collector" button, the instance of SpriteClass was
removed. This indicates the behaviour that I described in my example.

I changed the above example to use a weak listener. The instance of
SpriteClass was only removed after I clicked the "Run garbage
collector" button.

In the handler I removed the listener to the SpriteClass instance. The
instance of SpriteClass was only removed from memory after I clicked
the "Run garbage collector" button.

If I run the profiler with the
option "Generate object allocation stack traces", the instance is
removed everytime quite fast.

The conclusion of all this is that my statement in previous emails was
correct. What your example shows is that while the instance _can_ be garbage
collected, you will never know when it actually will.

A summary:

- There is no such thing as "a reference to be created invisibly within the
event system".
- You can not be sure when an instance is garbage collected.
- Weak references only indicate that: if the reference on the dispatcher
object to the listener is the last one, the instance containing the listener
will me available for garbage collection.

I want to thank you Cory for being as persistent as I can be. It enabled me
to do extensive tests to see
how the player works. In my tests in a standalone player there were
cases where I let the example application run for more then half an
hour without garbage collection of SpriteClass. I am not sure when the
garbage collector kicks in (might be triggered by total memory usage).
I will continue to monitor the
to see if the instance will ever be garbage collected. If not, I will
file a bug at adobe.

Greetz Erik

On 3/14/08, EECOLOR <[EMAIL PROTECTED]> wrote:
> Oh man, thank you for this example!
> This totally blows me away. I am sorry I was so persistent. You are
> correct. I will have to check all my applications now, hehe.
> This also means that all my earlier arguments are incorrect and that the
> use of Delegate is indeed very dangerous.
> Thank you for showing this to me! To me this really is unexpected
> behaviour... I just can not believe that this is designed behaviour...
> Greetz Erik
Flashcoders mailing list

Reply via email to