Anomalous behavior implementing the Dispose pattern?
-
I have a class which descends from Control and features a Bitmap property. It is my impression that it is my control's responsibility to call Dispose() on the example f_BitmapField in the commented out call of the following snippet. If I deploy this call (remove the comment instructions), there is no resource issue at run time destruction, but if this control is placed on a parent control, then when I delete the parent control at design time (without first deleting my child control) I get an ambiguous error stating only that a parameter is invalid. A later dialog is sometimes raised which refers to the property, not the field — suggesting that an invalid reference to the property exists. No Finalize method is deployed, and no further dispose processes refer to the field or resources which can be associated with it at disposal. Can anybody explain this behavior? Is it not theoretically the control's responsibility to call Dispose() on the bitmap field as in the commented lines here? I am beginning to wonder if the IDE isn't mishandling the proper implementation — forcing me to write the wrong code (omit dispose) so that I get acceptable design time behavior. Run time behavior doesn't seem to be affected. I don't feel comfortable about the required implementation. Maybe someone knows there is a different, explicit pattern to follow here?
protected override void Dispose( bool disposing ) { if ( disposing ) { // Release managed resources. // if ( null != f_BitmapField) // f_BitmapField.Dispose( ); } // Release unmanaged resources. // Set large fields to null. base.Dispose( disposing ); }