Eugeny89
Eugeny89

Reputation: 3741

flash garbage collector and handling events

take a look at the following code

var a = new View();
a = null;
....
class View {
    private var clip: MovieCLip

    public function View() {
        clip.addEventListener(...)
    }
}

will a be in memory after a = null? Does addEventListener adds a strong refernce?

Upvotes: 0

Views: 129

Answers (3)

Adrian Pirvulescu
Adrian Pirvulescu

Reputation: 4340

As you describe your example, the object where the event listener is attached will not be garbage collected. Even setting null will not help.

TO get this object goto gc() you can use one of the following approaches:

  1. useWeakReference

    clip.addEventListener(EVENT.name,listenerMethod,false,0,true);

  2. unsubscribe listener.

In handler method

function handlerMethod(ev:Event):void
{
  clip.removeEventListener(EVENT.name,listenerMethod);
}

Upvotes: 2

weltraumpirat
weltraumpirat

Reputation: 22604

Since all the references to clip are within a, GC will pick up both objects and cleanly remove them.

I've taken your example and used an ENTER_FRAME listener to create new Views in the same way you did:

Graph object not added to the stage

If, however, clip were added to the stage, then it would continue to exist, and a would also not be removed:

Graph for objects added to the stage

You can use the useWeakReference parameter of addEventListener to prevent this from happening.

Upvotes: 1

taskinoor
taskinoor

Reputation: 46037

By default addEventListener adds a strong reference. The last parameter of addEventListener is useWeakReference. You can use true for this parameter to specify a weak reference.

Upvotes: 2

Related Questions