Reputation: 3741
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
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:
useWeakReference
clip.addEventListener(EVENT.name,listenerMethod,false,0,true);
unsubscribe listener.
In handler method
function handlerMethod(ev:Event):void
{
clip.removeEventListener(EVENT.name,listenerMethod);
}
Upvotes: 2
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 View
s in the same way you did:
If, however, clip were added to the stage, then it would continue to exist, and a
would also not be removed:
You can use the useWeakReference
parameter of addEventListener
to prevent this from happening.
Upvotes: 1
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