Always Dispose System.Windows.Forms.DateTimePicker controls.

System.Windows.Forms.DateTimePicker registers for notification from the static SystemEvents.UserPreferenceChanged event.  It does this so that changes to the way a user would like to see date and time fields formated are reflected in the control as soon as the change has occured.  As this event is static, it is a GC root, so if you (as an object) register yourself for this event, the reference that the delegate chain of the event holds will keep you alive forever.  Obviously, de-registering for the event will remove this reference, which is what occurs in the Dispose method of the DateTimePicker.

An undiposed DateTimePicker will live for as long as the app domain does.

Posted: Mar 26 2006, 03:47 AM by nick | with 2 comment(s)
Filed under:

Comments

David M. Kean said:

I would extend this and say always dispose disposable objects, you never know what they are doing underneath, or will do underneath in future versions. Besides, you don't want the GC to have to run the finalizer if it has one.
# March 28, 2006 8:50 PM

nick said:

Totally agree David. I was making the point that not Disposing the DateTimePicker control is worse than a missed Dispose on some other Control-derived control.

BTW - congrats on the new job.
# March 30, 2006 8:23 PM