ActionScript 3: Do you need to remove EventListeners?

In actionscript 3, I dynamically create objects to which I add EventListeners. These objects are added to arrays and can be deleted later. And others may be added later. Every time I create an object, I add these EventListeners to them. However, is it necessary to delete these event listeners when deleting these objects? What happens when I lose all references to an object but don’t delete these EventListeners? Do they remain somewhere in the memory, inaccessible and untouched, or does the GC clear them?

+3
source share
2 answers

Yes, you should remove event listeners if you are not using weak links. The GC will not clear the object if there is a link to it, and registering event listeners creates a link to the object unless you set the parameter useWeakReference(parameter 5 th for addEventListener) before trueregistering the event listener. Weak links will not be counted by the garbage collector.

//Using strong reference: needs to be removed by calling removeEventListener
sprite.addEventListener(Event.TYPE, listenerFunction, useCaptureBool, 0, false);

//Using a weak reference: no need to call removeEventListener
sprite.addEventListener(Event.TYPE, listenerFunction, useCaptureBool, 0, true);
+4
source

When you have event listeners on an object, you will never lose all references to it, so it will remain in memory indefinitely. You must make sure that you always remove all listeners that you set. You can install them using weak links, but this is not quite a solution, it is better to remove them explicitly.

0
source

Source: https://habr.com/ru/post/1727345/


All Articles