Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

There are 4 categories of event subscriptions, all described below.

Instance to instance

This is the situation where an instance target subscribes to an instance event. The events are unbound as soon as either the target or source are collected.

Code Block
var source = new EventSource();
var listener = new EventListener();
var weakEventListener = WeakEventListener<EventListener, EventSource, EventArgs>.SubscribeToWeakEvent(listener, source, "PublicEvent", listener.OnPublicEvent);

Instance to static

This is the situation where a static target subscribes to an instance event. The events are unbound as soon as the source is collected.

Code Block
var source = new EventSource();

var weakEventListener = WeakEventListener<EventListener, EventSource, EventArgs>.SubscribeToWeakEvent(null, source, "PublicEvent", EventListener.OnEventStaticHandler);

Static to instance

This is the situation where an instance target subscribes to a static event. The events are unbound as soon as the target is collected.

Code Block
var listener = new EventListener();

var weakEventListener = WeakEventListener<EventListener, EventSource, EventArgs>.SubscribeToWeakEvent(listener, null, "StaticEvent", listener.OnPublicEvent);

Static to static

This is not supported because you shouldn’t be using a weak event listener here. Static events with static event handlers simply cannot cause memory leaks because both the source and the target have no instance. However, it might be possible that you subscribe to an event too many times and the event fires too many times. But again, no memory issues here.