Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When a listener attaches an event listener to an event, the source object will get a reference to the listener object. This means that the listener cannot be collected by the garbage collector until either the event handler is detached, or the source object is collected.</p> <p>Consider the following classes:</p> <pre><code>class Source { public event EventHandler SomeEvent; } class Listener { public Listener(Source source) { // attach an event listner; this adds a reference to the // source_SomeEvent method in this instance to the invocation list // of SomeEvent in source source.SomeEvent += new EventHandler(source_SomeEvent); } void source_SomeEvent(object sender, EventArgs e) { // whatever } } </code></pre> <p>...and then the following code:</p> <pre><code>Source newSource = new Source(); Listener listener = new Listener(newSource); listener = null; </code></pre> <p>Even though we assign <code>null</code> to <code>listener</code>, it will not be eligible for garbage collection, since <code>newSource</code> is still holding a reference to the event handler (<code>Listener.source_SomeEvent</code>). To fix this kind of leak, it is important to always detach event listeners when they are no longer needed.</p> <p>The above sample is written to focus on the problem with the leak. In order to fix that code, the easiest will perhaps be to let <code>Listener</code> hold on to a reference to <code>Source</code>, so that it can later detach the event listener:</p> <pre><code>class Listener { private Source _source; public Listener(Source source) { _source = source; // attach an event listner; this adds a reference to the // source_SomeEvent method in this instance to the invocation list // of SomeEvent in source _source.SomeEvent += source_SomeEvent; } void source_SomeEvent(object sender, EventArgs e) { // whatever } public void Close() { if (_source != null) { // detach event handler _source.SomeEvent -= source_SomeEvent; _source = null; } } } </code></pre> <p>Then the calling code can signal that it is done using the object, which will remove the reference that <code>Source</code> has to ´Listener`;</p> <pre><code>Source newSource = new Source(); Listener listener = new Listener(newSource); // use listener listener.Close(); listener = null; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload