Update on cause Error #2044: Unhandled ioError:. text=Error #2032: Stream Error

Hey All, seasoned AS3 programmer here.
Just wanted to share a recent encounter with Error #2044: Unhandled ioError which stumped me for ~20 minutes today:

So, this is a partial code class that will load and deserialize a custom data file, the error is reported at line:** loader = new URLLoader();**


...
        /**
         * Load and deserialize a FFON formatted object
         * @param    filepath
         * @param    callbackSuccess
         * @param    callbackError
         */
        public function Load(filepath:String, callbackSuccess:Function=null, callbackError:Function=null):void
        {
            loader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            
            loader.addEventListener(Event.COMPLETE, LoadSuccess, false, 0, true);
            
            loader.addEventListener(IOErrorEvent.IO_ERROR, LoadError, false, 0, true);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, LoadError, false, 0, true);
            loader.addEventListener(IOErrorEvent.NETWORK_ERROR, LoadError, false, 0, true);
            
            this.filepath = filepath;
            this.callbackSuccess = callbackSuccess;
            this.callbackError = callbackError;
            
            var url:URLRequest = new URLRequest(filepath);
            loader.load(url);
        }
        
        /**
         * Handle successful file load event, attempt to deserialize, and call the callback function if set.
         * Callback is called with a reference to this FFONSerializer object.
         * @param    evt   the complete event sent from a valid URLLoader object
         */
        public function LoadSuccess(evt:Event):void
        {
            var loader:URLLoader = URLLoader(evt.target);
            data = FFONSerializer.Deserialize(loader.data);
            
            if (callbackSuccess != null)
            {
                callbackSuccess(this);
            }
        }
        
        /**
         * Handle IOErrorEvent and SecurityErrorEvent in the case of being unable to load a file.
         * Callback is called with a reference to this FFONSerializer object.
         * @param    evt
         */
        public function LoadError(evt:Event):void
        {
            evt.stopPropagation();
            
            if (callbackError != null)
            {
                callbackError(this);
            }
        }
...

This is the harness used to test the above code, it has a success and a fail condition:


...
        public function RunTests():void
        {
            debugbox.text = "";
            LocalTrace("[Starting Tests]");
            
            var ffon:FFONSerializer;
            
            ffon = new FFONSerializer();
            ffon.Load("test1.ffon", FFONSuccess, FFONError);
            
            ffon = new FFONSerializer();
            ffon.Load("non_existant_file.ffon", FFONSuccess, FFONError);
        }
...

The error I was received was caused because var ffon:FFONSerializer; is disposed at the end of the RunTests(); function, and because I’d used weak references for my event listeners the event listener was being disposed before the load event had been fired, resulting in the error.

**loader.addEventListener(IOErrorEvent.IO_ERROR, LoadError, false, 0, [COLOR=DarkOrange]true[/COLOR]);

**So, two possible fixes. Either remove useWeakReferences flag from addEventListener, or, recode the RunTest method to store proper references to the FFONSerializer objects.

Like so:


...
        public var ffon1:FFONSerializer;
        public var ffon2:FFONSerializer;
        public function RunTests():void
        {
            debugbox.text = "";
            LocalTrace("[Starting Tests]");
            
            ffon1 = new FFONSerializer();
            ffon1.Load("test1.ffon", FFONSuccess, FFONError);
            
            ffon2 = new FFONSerializer();
            ffon2.Load("non_existant_file.ffon", FFONSuccess, FFONError);
        }

        public function FFONSuccess(ffon:FFONSerializer):void
        {
            LocalTrace("FFON File: " + ffon.filepath + " loaded OK");
        }
        
        public function FFONError(ffon:FFONSerializer):void
        {
            LocalTrace("FFON File: " + ffon.filepath + " failed to load");
        }
...

Now after RunTests executes, the asychnous IO_ERROR (file not found) executes correctly, and the correct trace message** “FFON File: non_existant_file.ffon**** failed to load”;** is displayed.

Just wanted to share this, since I didn’t find a reference to this possible (and probably quite obscure) cause in my googling.

I always recommend trying to handle the IO_ERROR event and other error events when accessing local files so that flash doesn’t hang unexpectedly if the network drops out or a file was put in the wrong folder.