0

I made a simple application in flex 3. The code is below.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
    <![CDATA[

        private function browseFile():void
        {
            var fr:FileReference  = new FileReference();
            fr.addEventListener(Event.SELECT, onFileSelect);
            fr.browse();
        }

        private function onFileSelect(evt:Event):void
        {
            trace(evt.currentTarget);
        }


    ]]>
</mx:Script>
    <mx:Button click="browseFile()" />
</mx:Application>

The case is that Event.SELECT never get fired. But if I make the fr reference of FileReference global(i.e. declared it outside any function), the Event.SELECT gets fired. Please Note that this happens only in Flex 3. In Flex 4, its working fine in both cases. Does It has to do something with Garbage Collection Mechanism in Actionscript? Can anybody explain please? I am just curious to know the reason.

Asad
  • 1,817
  • 16
  • 23

1 Answers1

0

Yes, it has everything to do with garbage collection. Since the fr (FileReference) variable is declared inside the function browseFile(), that variable only exists within the function. It therefore is eligible to be garbage collected as soon as the function finishes executing.

The fact that it seems to work in Flex 4 is a little strange, but keep in mind that garbage collection happens on Flash's schedule. So it could be just chance that in Flex 4 it doesn't get garbage collected right away (or maybe the Flex 4 app uses slightly less memory, so garbage collection doesn't happen as quickly). It could also be a bug in Flex 4, in that something is keeping a reference to the FileReference object, thus preventing it from being garbage collected.

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • If you use `Loader` or `URLLoader` in the same way as `FileReference` is used here, then `Event.COMPLETE` get fired properly. Then why this happens with only `FileReference`? – Asad May 29 '13 at 03:52
  • As I mentioned, Flash does garbage collection when it wants to. Another possible culprit is that something (in your code or in Flash Player itself) is keeping a reference to the object. One of the methods Flash uses for garbage collection is reference counting. If something else happens to have a reference to the object, then it won't be garbage collected. The best way to confirm what is happening is to use a profiler -- which can show which objects have references to other objects. I've seen scenarios where sometimes this type of code works, and sometimes it doesn't. – Sunil D. May 29 '13 at 05:42