2

Consider an AIR application which can load any number of third-party SWF files one at a time for display. Like a Web Browser.

The problem is that these SWF files may have EventListeners which have not been removed, etc.

Will Loader.unloadAndStop() be enough to garbage collect these SWFs?

If not- is there an approach which will (maybe a new NativeWindow for each SWF, then close the NativeWindow when done. Will that completely GC?)

davidkomer
  • 3,020
  • 2
  • 23
  • 58
  • You could investigate overriding addEventListener to keep a list of the listeners. If objects are listening to the stage, you might have a hard time overriding the default stage instance with one of your own, however. – Amy Blankenship Jul 31 '12 at 01:51

3 Answers3

2

Separate native window or loading inside a browser control can be a way for third party SWFs you have no control over. This adds a lot of overhead otherwise.

Unfortunately, you cannot ensure proper sandboxing of the loaded code (the display objects, most importantly, stage cannot be hidden from the loaded code). If the loaded code had added a listener to stage (which is a common thing to do if you need keyboard events), then it will not unload.

This is, however, impossible on mobile devices, where SWF format itself is different.

  • Will NativeWindow completely GC when closed, even if the third-party SWF is doing the worst possible deeds? – davidkomer Jul 30 '12 at 11:57
0

A while back I had a similar issue with externally developed components to which we didn't have the source, and it was sandwiched into our application. The best I could do / think of, was to do what the nuclear industry does - when things go bad, at least contain it in concrete so it doesn't spread more than it has to.

My solution was to build a 'component pool' so that as the contaminated objects were requested to be "clean" they were put in a separate holding area to be reused when needed - this way only the minimum number needed were ever created during a running session.

Mike Petty
  • 949
  • 6
  • 6
  • can this contain a memory leak in the external content? – Daniel Jul 31 '12 at 17:23
  • Yes to the extent that your application uses only the bare minimum of the affected area. For example, in that project we built a CMS based on pages from templates. With the standard approach, a page would be torn down and a 'new' would be constructed in it's place for new requests. With the pool approach, if you've already created 3 and are holding it's reference, and now request 5 - only 2 more 'new' are built. So the amount of leak depends on usage, but it usually models itself to a ln(e). – Mike Petty Jul 31 '12 at 17:58
  • My understanding is that the use of a loaded asset (source of which you have no control over) that has a memory leak could not be contained and could grow regardless. Also, in the example of a cms, you could have an asset that needs to reset when you open the containing element, and the reset can only happen reliably through a reload. – Daniel Aug 01 '12 at 16:21
  • In that specific case, the profiler and loitering object view both confirmed that while memory would rise, it did not rise arbitrarily with each successive operation that was a 'rinse-repeat'. It would only rise with each successive request for a new contaminated object that it did not already have. In this case, reseting any existing data was easy as the same methods to interact with it was the same as creating it and then setting it. Each element is different - but we were not dealing with views as obvious as adding another listener during a mouse.move event. – Mike Petty Aug 02 '12 at 02:48
-1

You should make a public static function to remove all the listeners in the loaded swf file(s) before you GC it.

Zhafur
  • 1,626
  • 1
  • 13
  • 31
  • 1
    As noted in the original question, these are for third-party SWFs, i.e. I have no knowledge or control of the listeners therein – davidkomer Jul 30 '12 at 11:55