8

Is it possible in Actionscript 3 to create a weak reference to an object, so that it can be garbage collected.

I'm creating some classes to make debugging easier, so I don't want the objects to hang around in memory if they are only referenced here (and of course I don't want to fill the code with callbacks to remove the objects)

Andres
  • 1,875
  • 1
  • 19
  • 28

2 Answers2

5

Grant Skinner has written an excellent series of articles on resource management in ActionScript 3, and in the third part of that series he introduces the WeakReference and the WeakProxyReference helper classes that can be used for this.

hasseg
  • 6,787
  • 37
  • 41
3

Right now I've made a simple class to take advantage of the Dictionary weakKeys parameter:

public class WeakReference
{
    private var dic

    public function WeakReference(object)
    {
        this.dic = new Dictionary(true)
        this.dic[object] = true
    }

    public function get Value()
    {
        for (var object in this.dic)
        {
            return object
        }
        return null
    }
}
Andres
  • 1,875
  • 1
  • 19
  • 28
  • @mattlohkamp Semi-colons aren't necessary sometimes, but it's a good pratice to use them. This applies to entire ECMAScript, they're empty statements, while commas make sequence expressions. E.g: `trace(1), trace(2);`. it works in both JS and AS3 (except that the global JS object doesn't have a getter/setter/property for `trace`). –  Dec 08 '16 at 12:07