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

后端 未结 11 1819
忘了有多久
忘了有多久 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:57

    No, IDisposable is for managed objects that use unmanaged resources. As a rule, you should always dispose of them when finished.

    0 讨论(0)
  • 2020-12-08 14:57

    Others have alluded to "using" blocks for GDI objects - here's a code example:

    using( var bm = new Bitmap() )
    using( var brush = new Brush() )
    {
    
       // code that uses the GDI objects goes here
       ...
    
    } // objects are automatically disposed here, even if there's an exception
    

    Note that you can have as many "using" lines as you want for a single code block.

    I think this is a nice, clean way to deal with Disposable objects.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 15:00

    No that is wrong. I agree with Aaronaught.

    In addition, Microsoft recommends, in a mid 2003 webcast that Don Box presented, that every .Net developer should dispose of their own objects, whether managed or unmanaged, as this improves code performance by anything upto 20%. If done right it can be a substantial performance improvement. So its a core skill that every .net developer needs to know and use.

    0 讨论(0)
  • 2020-12-08 15:10

    You need to dispose them.

    Managed object manage their memory by themselves. But memory isn't the only resource that an object uses. Dispose() is intended to release the other resources.

    0 讨论(0)
  • 2020-12-08 15:11

    You really need to look up the documentation for the brushes, pens etc.

    If they aren't using unmanaged resources, you may not have to call Dispose. But the using/Dispose pattern is sometimes "misused". As an example, consider the ASP.NET MVC framework. Here you can write something like:

    using(Html.BeginForm(...)){
      ///HTML input fields etc.
    }
    

    When Html.BeginForm(...) is called, a FORM tag will be output. When the using-statement ends, Dispose will be called on the object returned from Html.BeginForm(...). Calling Dispose causes the end FORM tag to be rendered. In this way the compiler will actually enforce the pairing of FORM tags, so you won't forget the closing tag.

    0 讨论(0)
提交回复
热议问题