<div class="gmail_quote">On Fri, Aug 24, 2012 at 10:50 AM, Jonathan Pryor <span dir="ltr"><<a href="mailto:jonpryor@vt.edu" target="_blank">jonpryor@vt.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="im">> It seems this could happen in more cases than just PInvoke. This seems to allow a finalizer to run before an object is "done being used" anytime the object instance is not stored. (i.e. inside a statement of the form "new Foo().Method();") If the finalizer triggers an IDispose pattern, this could cause a managed resource to be torn down before it's done being used as well.</div>

<div class="im">
<br>
</div>The managed resource can't be disposed before it's done being used AS LONG AS the GC knows about all uses of the managed resource.</blockquote><div><br></div><div>... snip ... </div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

The real problem is that the GC doesn't know anything about native code, and thus can't ensure that no native code is using the resource.</blockquote></div><div><br></div><div><div>Thanks very mych for the detailed reply. It seems to me there is a race that has nothing to do with native code. Consider this example..</div>

<div><br></div><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">class</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Foo</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">:</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">IDisposable</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">{</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">
   </span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">ManagedObject</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> mo </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">=</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">new</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">ManagedObject</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">();</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">

   </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">~</span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Foo</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">()</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">{</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">this</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">.</span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Dispose</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">();</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">}</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">
   </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">public</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">void</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Dispose</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">()</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">{</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">
       </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">if</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">(</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">mo </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">!=</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">null</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">)</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">{</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">
          </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">try</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">{</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">mo</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">.</span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Dispose</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">();}</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">finally</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">{</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> mo </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">=</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">null</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">;</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">}</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">
       </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">}</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">
   </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">}</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> 

   </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">void</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Problem</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">()</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">{</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> mo</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">.</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">doSomething</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">();</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">}</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">

   </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">static</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">void</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Main</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">()</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">{</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(0,0,136);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">new</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Foo</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">().</span><span class="typ" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,0,102);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">Problem</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">();</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">}</span><span class="pln" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">
</span><span class="pun" style="margin:0px;padding:0px;border:0px;font:inherit;vertical-align:baseline;color:rgb(102,102,0);font-family:arial,sans-serif;white-space:pre;background-color:rgb(255,255,255)">}</span>
</div><br>If I understand the <a href="http://MS.NET">MS.NET</a> article, as soon as ms.doSomething enters the vcall, "this" is no longer referenced. Which means during ManagedObject.DoSomething, Foo could be finalized, and thus Disposed, and since the Dispose explicitly Disposes mo, the code would Dispose mo while it's still inside mo.doSomething(). Did I miss something?<br>

<br> <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
> Why isn't this considered a bug in the .NET runtime?<br>
<br>
</div>How would you fix it? The .NET runtime has no way of knowing what native code is doing, so short of disassembling the native code ("magic"), what is .NET supposed to do?<br></blockquote><div><br></div><div>

Ohh, I don't think the problem is the way this is handled for native code. I think the above interaction in IDisposable seems like a problem too. To me this seems like a pre-mature finalization bug caused because "this" isn't considered referenced for the entire body of instance methods.</div>

<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
> (2) Does the Mono GC have the same behavior?<br>
<br>
</div>Yes, because there's no other sane behavior.<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">However, this can't be relied upon; Linux supports "precise stack marking," which prevents conservative scanning of native stack frames. This has the wonderful performance advantage that less memory needs to be pinned, allowing the GC to be more efficient:<br>


<br>
        <a href="http://www.mono-project.com/Generational_GC#Precise_Stack_Marking" target="_blank">http://www.mono-project.com/Generational_GC#Precise_Stack_Marking</a></blockquote><div><br></div><div>I'm sorry for my naivety. Why does allowing unused function arguments to be collected before a function returns have such important effects on memory usage? </div>

<div> </div><div><br></div></div>