1

A default form Dispose function will look something like this:

protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

The normal Dispose Pattern calls for releasing your managed resources:

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            // Free other state (managed objects).
        }
        // Free your own state (unmanaged objects).
        // Set large fields to null.
        disposed = true;
    }
}

Including deregistering from events:

Is it good practice to unregister from external events in the Dispose method of an IDiposable class?

Is it bad to not unregister event handlers?

Should I always disconnect event handlers in the Dispose method?

As far as I understand it, the purpose of unregistering events is to break the circular reference that keeps the garbage collector at bay:

Form holds a reference to the component, and the event subscription gives the component a reference to the form

But many opinions state that you don't need to break this loop if the item you've subscribed to is "short lived" or owned by the form and built with the designer.

Does components.Dispose() or base.Dispose(disposing) somehow set my local private System.Windows.Forms.Button button1 reference to null or unregister my event handler this.button1.Click += new System.EventHandler(this.button1_Click);? It doesn't look like components is ever initialized, so components.Dispose() won't even be run? We want button1 to be short lived and go away with the form, but don't we have a reference loop here that prevents the garbage collector from cleaning up either?

Community
  • 1
  • 1
Denise Skidmore
  • 2,286
  • 22
  • 51
  • You only need to unregister events if the event publisher will outlive the subscriber. That's because the publisher would keep a reference to the subscriber via the event handler, and thus the subscriber wouldn't be eligible for GC. In the case of events on a single form (i.e. your every-day button click events), both the subscriber and the publisher will be eligible for GC at the same time, when the form is no longer referenced. The GC knows that the form is no longer referenced, so therefore everything within is also eligible for collection, including any circular event references. – Glorin Oakenfoot Dec 28 '15 at 20:23
  • "The GC knows that the form is no longer referenced" This is the part I'm struggling with, because isn't the form referenced by the button after event registration? The reference count isn't 0. – Denise Skidmore Dec 28 '15 at 20:27
  • The GC doesn't count references. It tracks whether or not something can be reached from a 'live' part of the program. See the answer [here](http://stackoverflow.com/a/8840596/4372746) for a rough overview. – Glorin Oakenfoot Dec 28 '15 at 20:30
  • Ah... MS source on the roots: https://msdn.microsoft.com/en-us/library/ms973837.aspx... – Denise Skidmore Dec 28 '15 at 20:47
  • 1
    @GlorinOakenfoot - comments don't count on SO, you have enough here for an answer. – H H Jan 02 '16 at 14:04

0 Answers0