Events are now handled by adding an EventListener to an EventHandler which gets
registered to the EventDispatcher. The EventHandlers are only weakly referenced inside
the EventDispatcher.
What changed
Previously events were handled by registering an EventListener to the EventDispatcher.
Now events are handled by creating an EventHandler, registering that EventHandler to the
EventDispatcher and then registering an EventListener to the EventHandler.
EventDispatcher is still a singleton. There can be arbitrarily man EventHandlers added
to the EventDispatcher and arbitrarily many EventListeners added to one EventHandler.
The important thing:
EventHandlers are only weakly-referenced by the EventDispatcher. The code that registers
them have to keep a reference to the EventHandler in order to prevent them from being
garbage collected.
Why:
EventListeners often keep references to objects. That way, if the EventListener isn't
eventually removed from the EventDispatcher it stays alive forever together with all of
the objects if references directly or indirectly, thus leaking memory. (Leaking memory here
means that memory isn't freed even though it's not needed anymore. This differs from the
C-meaning of a memory leak where the memory can't be referenced anymore. The memory can
still theoretically be referenced, but isn't by the program.)
Now the calling code can (and must keep a reference to the EventHandler inside some object
to which that EventHandler is meaningfully associated. If that object gets garbage
collected, there won't be any strong reference to the EventHandler anymore (since it's
only weakly referenced by the EventDispatcher, using a WeakReference<EventHandler>) and
the EventHandler will therefore be garbage collected as well.
TL;DR
Event
s are now handled by adding anEventListener
to anEventHandler
which gets registered to theEventDispatcher
. TheEventHandler
s are only weakly referenced inside theEventDispatcher
.What changed
Previously events were handled by registering an
EventListener
to theEventDispatcher
. Now events are handled by creating anEventHandler
, registering thatEventHandler
to theEventDispatcher
and then registering anEventListener
to theEventHandler
.EventDispatcher
is still a singleton. There can be arbitrarily manEventHandler
s added to theEventDispatcher
and arbitrarily manyEventListener
s added to oneEventHandler
.The important thing:
EventHandler
s are only weakly-referenced by theEventDispatcher
. The code that registers them have to keep a reference to theEventHandler
in order to prevent them from being garbage collected.Why:
EventListener
s often keep references to objects. That way, if theEventListener
isn't eventually removed from theEventDispatcher
it stays alive forever together with all of the objects if references directly or indirectly, thus leaking memory. (Leaking memory here means that memory isn't freed even though it's not needed anymore. This differs from the C-meaning of a memory leak where the memory can't be referenced anymore. The memory can still theoretically be referenced, but isn't by the program.)Now the calling code can (and must keep a reference to the
EventHandler
inside some object to which thatEventHandler
is meaningfully associated. If that object gets garbage collected, there won't be any strong reference to theEventHandler
anymore (since it's only weakly referenced by theEventDispatcher
, using aWeakReference<EventHandler>
) and theEventHandler
will therefore be garbage collected as well.