Griallia
Griallia

Reputation: 171

(AS3) Is removeEventListener Necessary?

Is removeEventListener as necessary as dealloc?

I know it's possible to get memory leaks in AS3 and I'm just wondering if not removing Event Listeners is one of those ways.

Upvotes: 1

Views: 1723

Answers (2)

Marcx
Marcx

Reputation: 6836

Yes, I created a huge project without removing any eventListener, and after long use it really get a lot of memory...

Any object with a strong reference will not be garbage collected, until the strong reference is removed.... this apply to eventlistener, bindings, etc...

You can removeEventListener manually using the removeEventListener(Event.TYPE, function) or using the weakReference...

Adding an Event Listener with WeakReference you simply need to add more parameters to the addMethod...

obj.addEventListener(Event.Type, Function, false, 0, true)

addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void

Parameters:
type The type of event.
listener The listener function that processes the event. This function must accept an event object as its only parameter and must
return nothing, as this example shows:
function(evt:Event):void

The function can have any name.
useCapture Determines whether the listener works in the capture phase or the target and bubbling phases. If useCapture is set to true,
the listener processes the event only during the capture phase and not in the target or bubbling phase. If useCapture is false, the listener processes the event only during the target or bubbling phase. To listen for the event in all three phases, call addEventListener() twice, once with useCapture set to true, then again with useCapture set to false.
priority The priority level of the event listener. Priorities are designated by a 32-bit integer. The higher the number, the higher the priority. All listeners with priority n are processed before listeners of priority n-1. If two or more listeners share the same priority, they are processed in the order in which they were added. The default priority is 0.
useWeakReference Determines whether the reference to the listener is strong or weak. A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.
Class-level member functions are not subject to garbage

Upvotes: 4

Florian Salihovic
Florian Salihovic

Reputation: 3961

Yes it is, because you 'bind' one instance into the scope of another. Thus, when handling events incorrectly, you'll get cross references all over the application and the garbage collection won't find any object, which could be freed from memory.

Upvotes: 4

Related Questions