package com.exanimo.net { import com.exanimo.utils.GarbageCollectionShield; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.net.URLLoader; import flash.net.URLRequest; /** * * A URLLoader that refuses to be garbage-collected. * * @see http://exanimo.com/actionscript/never-use-tween-or-urlloader/ * * @author Matthew Tretter (matthew@exanimo.com) * @version 2007.07.26 * */ public class GCSafeURLLoader extends URLLoader { private static var _gcShield:GarbageCollectionShield = new GarbageCollectionShield(); /** * @copy flash.net.URLLoader#URLLoader() */ public function GCSafeURLLoader(request:URLRequest = null) { // Add listeners so that we know when to start allowing garbage // collection again. this.addEventListener(Event.COMPLETE, GCSafeURLLoader._allowGarbageCollection); this.addEventListener(IOErrorEvent.IO_ERROR, GCSafeURLLoader._allowGarbageCollection); this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, GCSafeURLLoader._allowGarbageCollection); super(request); } // // public methods // /** * @inheritDoc */ public override function close():void { GCSafeURLLoader._gcShield.remove(this); super.close(); } /** * @inheritDoc */ public override function load(request:URLRequest):void { GCSafeURLLoader._gcShield.add(this); super.load(request); } // // private methods // /** * * Removes a loader from the shield so that the garbage collector can * grab it. * */ private static function _allowGarbageCollection(e:Event):void { GCSafeURLLoader._gcShield.remove(e.currentTarget); } } }