Do I have to set a removeEventListener when I called addEventListener? - actionscript-3

I'm a beginner of ActionScript 3. Recently I'm trying to use addEventListener to invoke function. I found that there are some examples add a removeEventListener when they invoke functions, such as:
public function Away3DMultiMarkerDemo()
{
addEventListener(Event.INIT, initIN2AR);
super();
}
private function initIN2AR(e:Event = null):void
{
removeEventListener(Event.INIT, initIN2AR);
in2arLib.init( workW, workH, maxPoints, maxReferences, 100, stage );
in2arLib.setupIndexing(12, 10, true);
in2arLib.setUseLSHDictionary(true);
in2arLib.addReferenceObject( ByteArray( new DefinitionaData0 ) );
in2arLib.addReferenceObject( ByteArray( new DefinitionaData1 ) );
in2arLib.setMaxReferencesPerFrame(2);
in2arLib.setMatchThreshold(40);
intrinsic = in2arLib.getIntrinsicParams();
initCamera();
initAway3D();
initText();
initListeners();
}
My question is that do I need to set a removeEventListener each time when I called addEventListener? I did some research that the purpose of adding the removeEventListener is to release memory, otherwise program will keep listen events.

It is good practice to remove your listeners when you no longer need them. But that is a call you must make in each situation.
Adding an event listener by default will hang onto a reference of the thing it is added to. So if you add a listener to a movieclip, and delete that movieclip, it will not be garbage collected because the event listener still has a reference to it. For this reason it is good to remove any listeners on an object as part of your deletion process. Of course you can also use the "weak reference" argument in the addEventListener method, so the listener will not keep the garbage collector from destroying the object.
In the case of the Event.INIT event in your example; That should only ever fire once, so the event handler is the perfect place to make sure you remove the listener.

No. You only have to do this if you want the event to execute only once. You also call it when you no longer need the listener, so that it doesn't waste memory.
If you call it as the very first statement in the function that is called when the event is fired, it will ensure that the listener is only called once.

Related

AS3, clarification on the 'this' pointer

I am writing a program in flex, and I am having an issue with the this pointer.
I have a function call inside of a function and I am wondering which event my this pointer is passing.
private function funcA(someArgument)
{
newDialog.show("Title", "Body", funcB, this) // the 'this' is the argument for funcB
}
private function funcB(CloseEvent:event)
{
if(event == Event.CANCEL)
doStuff;
if(event == Event.COMPLETE)
doOtherStuff;
}
To clarify, my question is, when I pass the this pointer into the function in the newDialog, am I passing my newly created Dialogue or am I passing in funcA. I'm thinking and hoping its the former.
I would just test this manually but the system is really large and currently tied in with other events so its difficult for me to just instantiate objects to test them.
While null is correct, the method signature on funcB looks like it should be added as an event listener. funcB is expecting an event. All events have a property target, which is the actual object that dispatched the event, and currentTarget, which is the object that was being listened to. These will only be different in the case of bubbling events on the display list, where a child object really dispatched the event.
If the Class you found this code in is not a subclass of Event, I would expect funcB to error pretty hard. If it is a subclass of Event, it shouldn't be, because the functionality in funcA is nothing that should ever be the responsibility of an event.
when I pass the this pointer into the function in the newDialog, am I passing my newly created Dialogue or am I passing in funcA.
Neither one.
You have a private function, which is a member of a class. In order to call that function, an object has to be created that's an instance of that class and then funcA can be called on that object.
this is a reference to exactly that object.

Set event.target to null

I want to nullify the event.target for garbage collection as the moveclip is no longer needed. What I have roughly is this:
mc.addEventListener(MouseEvent.CLICK, destroy);
public function destroy(event:MouseEvent):void {
event.target.parent.removeChild(event.target);
event.target.removeEventListener(MouseEvent.CLICK, destroyShape);
event.target = null; //THIS IS WHAT I WANT TO ACHIEVE
}
I'm sure this is relatively simple but I'm not sure how to do it.
thanks
You can't change MouseEvent.target value. It's a read only property. If your MovieClip doesn't exist (removeChild) and you removed event handler
mc.removeEventListener(MouseEvent.CLICK, destroy);
then the garbage collector automatically will remove it.
What you are trying to achieve (if it was even possible) would achieve nothing in term of Garbage Collection.
Unless redispatched the event will be garbage collected as soon as the method is done running. All properties of the event will also be discarded. This garbage collection of the event itself and its properties will have absolutely NO EFFECT on the object they point to in term of Garbage collection.
In the scope at which the event was dispatched in the first place the object will continue to exist after the event itself has been discarded. That is at that scope that the object reference must be nullified, not at the event listener scope where it will have no effect since the object still exists at the dispatching scope.
The solution accepted also does nothing. It is as relevant as applying a bandage to a wooden leg. Any local variable in function/method qualify for GC immediately after the method runs. Nullifying those variables has no effect whatsoever and does not constitute a valid answer to any problem and certainly not a GC problem. That those variables are GC does also not constitute a guaranty that the object they point to will be GC.
This is a case where a question about an inexistent and misunderstood problem is asked and answered by posting a false and irrelevant solution.
Case in point: Only a DisplayObject currently in a display list and attached to the stage has the ability to be the target of a MouseEvent. It is simply impossible for that object to become available for garbage collection before it is removed from the display list it belongs to. For those reasons that object cannot qualify for GC at the time the MouseEvent listener runs since that object still has at least one strong reference because it is attached to a display list. This is the proof that what the PO asks is misguided and any code examples are misguided as well since they cannot qualify the object for GC at that point in time.
As #subdan states, the target property of any Event is a readonly property.
You can still null your movie clip but not like in your sample.
mc.addEventListener(MouseEvent.CLICK, destroy);
public function destroy(event:MouseEvent):void
{
var myMC:MovieClip = event.target as MovieClip;
if( myMC )
{
myMC.removeEventListener(MouseEvent.CLICK, destroyShape);
myMC.parent.removeChild(myMC);
myMC = null; //THIS IS WHAT I WANT TO ACHIEVE
}
}

Want to clear some doubts regarding Garbage Collection ( AS3.0)

package
{
public class SomeClass
{
public var myBtn:Button ;
public function SomeClass()
{
myBtn.addEventListener( MouseEvent.CLICK, clickFunction) ;
}
function clickFunction(e:Event){
}
}
}
Main Class:
var someClass:SomeClass = new SomeClass(); // step 1
addChild(someClass); // step 2
removeChild(someClass); // step 3
someClass = null // step 4
In the above, I want to completely destroy the someClass instance, so in step 4 it's been assigned null value.
Q1) Is step 4 ( assignment to null ) right way to destroy the instances ?
Q2) I referred : http://gskinner.com/blog/archives/2006/06/as3_resource_ma.html using delete keyword for objects. But i don't think delete keyword can be used for class instances ? How to use it in this case then ?
Q3) What happens to myBtn eventListener in SomeClass. Should i add a removeEventListener manually, before destroying instances of SomeClass. Or would it get destroyed automatically ?
Thanks
1/ Yes. (I assume the someClass variable is an instance variable)
2/ delete does not only remove the value of a variable, but even the variable definition. Since classes in AS3 are sealed in general, it won't work. I.e. delete should only be used on dynamically created members. Mainly "keys" of Object or Dictionary instances. But obviously you can remove the member definition of any class marked as dynamic using delete.
3/ Yes, always remove event listeners manually. You can create weakly referenced event listeners when registering the handler as a listener, but it's best to always make sure event listeners are removed manually, it's more readable, clear and fail-safe.
I tend to have a destroy method in all my classes, which can be called by the instance owner when it's cleaning up its references to a certain instance. This destroy method will unregister all event listeners, nullify instance members and do all kinds of cleanup.
The SomeClass instance you created will get garbage-collected after there are no longer any references to it. By setting your variable to null, it removes that reference, and the SomeClass instance will get garbage-collected as long as there are no more references to it.
In the code above, you do not need to remove the event listener. When a SomeClass instance is collected, all of its member variables will be collected (unless they're referenced elsewhere). Because the event listener has been added to the button, it will be collected when the button is collected.
Something to note, however: if instead of myBtn.addEventListener you had used stage.addEventListener, the stage would retain a reference to your callback function and you could end up with a memory leak. Always remove event listeners that are assigned to objects that will still be around after you care about the listener.
Creynder's advice is good: remove event listeners as a matter of habit. It's only really necessary, however, when an event listener's dispatcher is going to hang around but you don't want the callback to stay in memory.

Does addEventListener (and removeEventListener) stack?

Given the below:
object.addEventListen(eventType01,handler01);
object.addEventListen(eventType01,handler01);
object.removeEventListener(eventType01,handler01);
How many event listeners for eventType01 are on object? One or zero?
Zero. If you call addEventListener, using the exact same arguments more than once all subsequent calls after the first "silently fail." Call add as many times as you want, but that single remove will wipe the listener away.
EDIT: Another thing to keep in mind is that there's no penalty to calling multiple identical removeEventListener() functions on the same object, aside from needless performance overhead. No deadly error will occur, it will simply "silently fail" much the same way as repeated identical addEventListener calls will.
EDIT 2: To answer #ThomasM :: if your listener "fires twice" then you do not have exactly the same listener. Try putting this quick and dirty code on frame 1 in a fla that has one square movieclip as a child:
import flash.events.*
function foo(e):void{
trace("hi");
}
this.addEventListener(MouseEvent.CLICK,foo);
this.addEventListener(MouseEvent.CLICK,foo);
this.addEventListener(MouseEvent.CLICK,foo);
Notice that your output when you click the movieclip is precisely one trace action.
Now add this line to the end of the code
this.removeEventListener(MouseEvent.CLICK,foo);
Even though you added foo as a listener for click 3 times, this one call kills the listener completely.
So if you're experiencing an issue where "the same listener" fire twice then probably what you're doing is something like this:
this.addEventListener(MouseEvent.CLICK, function(e){
trace("hi");
});
this.addEventListener(MouseEvent.CLICK, function(e){
trace("hi");
});
That will definitely "fire twice" because the listener functions ARE NOT THE SAME. They perform the same actions but they do not reference identical positions in memory. They do not point to the same function object. Furthermore this is highly bad practice because you have no way to actually remove those listeners. How would you do it? You have no reference to them.

Will duplicate addEventListener calls create duplicate listener entries?

If I have an object that calls
addEventListener(Event.ENTER_FRAME, update);
addEventListener(Event.ENTER_FRAME, update);
will that add 2 listeners?
Nope, they won't, so update will only be called once when the event triggers.
Depends on what you're attaching the listeners to. If you attach to movieClipX and to movieClipY, you'll have two listeners,so if one of the mcs is removed you'll still have the other listener. If you attach the same listener to the same object twice,it'll behave as a single listener.
Also, to complete on the already provided answers, if you do:
addEventListener(Event.ENTER_FRAME, update1);
addEventListener(Event.ENTER_FRAME, update2);
Then it will execute BOTH functions. The later addEventLister will NOT overwrite the previous one, but add to the existing listeners, as the name of the method implies (except if the listener function was already added, in which case it will do nothing, as already stated in the accepted answer).