Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>...you’ll notice that managed resources are not handled if disposing is false. How/when are they handled?</p> </blockquote> <p>You didn't include it in your sample, but often the type will have a destructor which will call <code>Dispose(false)</code>. Thus, when <code>disposing</code> is <code>false</code>, you "know" that you're in a finalizer call, and thus <em>should</em> *not* access any managed resources, <em>because</em> they might have <em>already</em> been finalized.</p> <p>The GC finalization process only ensures that finalizers are invoked, not the <em>order</em> that they're invoked in.</p> <blockquote> <p>what does the GG.SuppressFinalize(this) call do?</p> </blockquote> <p>It prevents the GC from adding your object to the finalization queue and eventually calling <code>object.Finalize()</code> (i.e. your destructor). It's a performance optimization, nothing more.</p> <blockquote> <p>The way the pattern is written, it seems they would get disposed (later) if you did nothing in the “if (disposing)” section</p> </blockquote> <p><em>Maybe</em>; it depends upon how the type is written. A major point to the <code>IDisposable</code> idiom is for "deterministic finalizaion" -- saying "I want your resources freed <em>now</em>" and having it mean something. If you "ignore" the <code>disposing=true</code> block and don't "forward" the <code>Dispose()</code> call, one of two things will happen:</p> <ol> <li>If the type has a finalizer, the finalizer for the object may eventually be invoked sometime "later".</li> <li>If the type doesn't have a finalizer, the managed resource will "leak," as <code>Dispose()</code> will never be invoked on them.</li> </ol> <blockquote> <p>it's important that you never acquire locks or use locking during your unmanaged cleanup.” Why is that? Does it apply to unmanaged resources as well?</p> </blockquote> <p>It's a sanity issue -- YOUR sanity. The simpler your cleanup code, the better, and it's <em>always</em> a good idea to <em>not</em> throw exceptions from <code>Dispose()</code>. Using locks can result in exceptions or deadlocks, either of which are good ways to ruin your day. :-)</p> <blockquote> <p>does on ever implement a finalizer (~MyClass() in C#) without implementing IDisposable</p> </blockquote> <p>One could, but it would be considered bad style.</p>
 

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