Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a weak reference to an object

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)

like image 987
Andres Avatar asked Nov 03 '08 12:11

Andres


People also ask

How do you create a weak reference in Java?

WeakReference Class In Java. When we create an object in Java, an object isn't weak by default. To create a Weak Reference Object, we must explicitly specify this to the JVM.

What is a weak reference C++?

In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector, unlike a strong reference.

How do you use weak references in Python?

To create weak references in python, we need to use the weakref module. The weakref is not sufficient to keep the object alive. A basic use of the weak reference is to implement cache or mappings for a large object. Not all objects can be weakly referenced.

What is the purpose of a weak reference?

A weak reference allows the garbage collector to collect an object while still allowing an application to access the object. If you need the object, you can still obtain a strong reference to it and prevent it from being collected.


2 Answers

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.

like image 126
hasseg Avatar answered Oct 05 '22 19:10

hasseg


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
    }
}
like image 31
Andres Avatar answered Oct 05 '22 19:10

Andres