flash event_model_weak_references
Weak references
In ActionScript 3, the memory management is assured by a part of the player called the Garbage Collector. We touched on this in a previous article called Language and API.
The garbage collector is there to liberate resources by deleting objects from the memory that are no longer used. An object is considered as unused when no reference points towards it, or in other words when an object becomes inaccessible.
When we listen to a specific event, the listener is added to an internal broadcast list, which is in reality an array storing the references of each listener.
The target references the listener and not the other way around.
Because of this the garbage collector will never delete its listeners as long as they are referenced by the targets and as long as they remain referred.
At the time of subscription of a listener to an event you can modify this behavior thanks to the last addEventListener parameter method.
Let’s get back to the signature of this method:
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
The fifth parameter called useWeakReference allows you to specify if the listener function will use a strong or weak reference. When using a weak reference, the listener function will not be placed in the internal listener array and won’t therefore be referenced by the subject.
If the only or last reference to the listener function is a weak reference, the garbage collector makes an exception and ignores the reference and deletes the listener from the memory.
Beware, this does not mean that the listener function will be obligatorily deleted from the memory. It will only be deleted if the garbage collector intervenes and proceeds with a clean up.
Note that this intervention could never happen if the Flash player didn’t decide to do this.
It is for this reason that you shouldn’t consider this technique as an automatic deletion of listening functions once they are no longer useful. Remember to always make the removeEventListener method delete the listening of certain events when it is no longer necessary.
If we don’t specify this parameter its default value is false. This signifies that all our subject objects keep a strong reference to the listener until the removeEventListener method is called.
In the following code an instance method of the personalized class is saved as an event listener Event.ENTER_FRAME of a MovieClip:
// instancing of a personalized object var myPersonalisedObject:PersonalisedClass = new PersonalisedClass(); var myClip:MovieClip = new MovieClip(); // listens to the Event.ENTER_FRAME event myClip.addEventListener( Event.ENTER_FRAME, myPersonalisedObject.listener ); // deletes the reference to the instance of the PersonalisedClass myPersonalisedObject = null;
Even though we have deleted the reference to the instance of the PersonalisedClass, it remains referenced by the subject object myClip.
By subscribing ourselves as the listener with the help of a weak reference, the garbage collector will ignore the reference retained by the subject and will delete the instance of the PersonalisedClass in the memory:
// listening to the Event.ENTER_FRAME event myClip.addEventListener( Event.ENTER_FRAME, myPersonalisedObject.listener, false, 0, true );
Weak references are very interesting when a listener doesn’t know the subject object to which it is subscribed. Having no way of unsubscribing to the event, it is recommended to use a weak reference in order to guarantee that the object can be deleted.
We will come back to these subtleties throughout these articles in order to avoid being caught out by the garbage collector.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


