6 Replies Latest reply on Nov 22, 2014 5:56 AM by rjanma

    Memory leak using URLStream

    bbitmmstr

      I have an Air application that needs to loop through thousands of URLs to download and save images to the user's local drive.  I am able to get through a few hundred but receive the following error:

      Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.

       

      Others have mentioned that this error means that the URL doesn't exist or is invalid but I can guarantee that the URLs are valid.

       

      Here is how my class is getting called:

          var fp:FileProcessor = new FileProcessor(imgPath, dirPath);

          fp.addEventListener(Event.COMPLETE, imgDownloadComplete);

          fp.startDownload();

       

      And in imgDownloadComplete, after updating the progress bar I am calling:

       

        var fp:FileProcessor = event.currentTarget as FileProcessor;

        fp.removeEventListener(Event.COMPLETE, imgDownloadComplete);

        fp = null;

       

      Finally, FileProcessor looks like:

      public class FileProcessor extends EventDispatcher {

                          private var urlPath:String;

                          private var dirPath:String;

                          private var urlStream:URLStream;

                          private var fData:ByteArray;

                          private var file:File;

       

                          public function FileProcessor(url:String, path:String) {

                                    this.urlPath = url;

                                    this.dirPath = path;

                   //////This next line is where the error above points to

                   urlStream = new URLStream;

                   urlStream.addEventListener(ProgressEvent.PROGRESS, onProgress);

                   urlStream.addEventListener(Event.COMPLETE, onComplete);

                          }

       

                          public function startDownload():void {

                                    fData = new ByteArray;

                                    urlStream.load( new URLRequest(this.urlPath) );

                          }

       

                          private function onProgress(event:ProgressEvent):void {

                                    if(urlStream.bytesAvailable == 0) return;

                                    if(urlStream.connected) urlStream.readBytes(fData, fData.length);

                          }

       

                          private function onComplete(event:Event):void {

                                    if(urlStream.connected)

                                              urlStream.close();

       

                                    var fileStream:FileStream = new FileStream;

                                    file = File.desktopDirectory.resolvePath(this.dirPath);

                                    fileStream.open(file, file.exists ? FileMode.APPEND:FileMode.WRITE);

                                    fileStream.writeBytes(fData);

                                    fileStream.close();

       

                                    fData.clear();

                                    fData = null;

                                    file = null;

                                    fileStream = null;

       

                                    urlStream.removeEventListener(ProgressEvent.PROGRESS, onProgress);

                                    urlStream.removeEventListener(Event.COMPLETE, onComplete);

       

                                    urlStream = null;

                                    dispatchEvent(new Event(Event.COMPLETE));

                          }   

       

                          private function dispatchComplete():void {

                                    dispatchEvent(new Event(Event.COMPLETE));

                          }

                }

      }

       

      ANY help or pointers that somebody can give would be greatly appreciated.  This is my first shot at an Air/Flex application so I am unsure where to look.  My company wouldn't purchase the Professional FlexBuilder so I do not have access to the profiler.