Do I need to call Dispose() on managed objects?

后端 未结 11 1868
忘了有多久
忘了有多久 2020-12-08 14:51

I can\'t believe I\'m still confused about this but, any way, lets finally nail it:

I have a class that overrides OnPaint to do some drawing. To speed things up, I c

11条回答
  •  一个人的身影
    2020-12-08 14:58

    Although you asked about pens and brushes, Font is a class with some odd quirks. In particular, if one creates a font for the purpose of setting a control's Font property, one remains responsible for disposing of that font--ownership does not transfer to the control--but that responsibility can be carried out by disposing of the font at any time--even as soon as the font is created, before assigning it to the control. It seems Font is a combination of a managed information object and an unmanaged GDI resource, and for some purposes only the former is needed. Weird design--Font should have been two classes.

提交回复
热议问题