Note that there are some explanatory texts on larger screens.

plurals
  1. PODealing with .NET IDisposable objects
    primarykey
    data
    text
    <p>I work in C#, and I've been pretty lax about using <code>using</code> blocks to declare objects that implement <code>IDisposable</code>, which you're apparently always supposed to do. However, I don't see an easy way of knowing when I'm slipping up. Visual Studio doesn't seem to indicate this in any way (am I just missing something?). Am I just supposed to check help every time I declare anything, and gradually build up an encyclopedic memory for which objects are and which are not disposable? Seems unnecessary, painful, and error-prone.</p> <p>How do <em>you</em> handle this?</p> <p><strong>EDIT:</strong></p> <p>Looking at the related questions sidebar, I found <a href="https://stackoverflow.com/questions/2785/setting-objects-to-nullnothing-after-use-in-dot-net">another question</a> which made it clear that <code>Dispose()</code> is supposed to be called by the object's finalizer anyway. So even if you never call it yourself, it should eventually happen, meaning you won't have a memory leak if you don't use <code>using</code> (which is what I suppose I was really worried about all along). The only caveat is that the garbage collector doesn't know how much extra memory is being held by the object as unmanaged stuff, so it won't have an accurate idea how much memory will be freed by collecting the object. This will result in less-ideal-than-usual performance by the garbage collector.</p> <p>In short, it's not the end of the world if I miss a <code>using</code>. I just wish something would generate at least a warning for it.</p> <p>(Off-topic: why is there no special markdown for linking to another question?)</p> <p><strong>EDIT:</strong></p> <p>Ok, fine, stop clamoring. It's super duper all-fired <a href="http://www.youtube.com/watch?v=a1Y73sPHKxw" rel="nofollow noreferrer">dramatic-chipmunk</a>-level <strong><em>important</em></strong> to call <code>Dispose()</code> or we'll all <em>die</em>.</p> <p>Now. Given that, why is it so easy &mdash; hell, why is it even <em>allowed</em> &mdash; to do it wrong? You have to go out of your way to do it right. Doing it like everything else results in armageddon (apparently). So much for encapsulation, huh?</p> <p>[Stalks off, disgusted]</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload