<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.kwd
        {mso-style-name:kwd;}
span.pln
        {mso-style-name:pln;}
span.typ
        {mso-style-name:typ;}
span.pun
        {mso-style-name:pun;}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Am I wrong in thinking that in<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">  
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">void</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Problem</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">()</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
 mo</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">.</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">doSomething</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">();</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">}</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">"mo" is contained within the context of the method body of Problem() and therefore cannot be disposed of until the method body of Problem() has done execution. 
 This means that "mo" will continue to live for the life of the call to doSomething() because the Problem() method body holds onto "mo" until after doSomething() returns.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> mono-devel-list-bounces@lists.ximian.com [mailto:mono-devel-list-bounces@lists.ximian.com]
<b>On Behalf Of </b>David Jeske<br>
<b>Sent:</b> Friday, August 24, 2012 1:27 PM<br>
<b>To:</b> Jonathan Pryor<br>
<b>Cc:</b> mono-devel-list@lists.ximian.com<br>
<b>Subject:</b> Re: [Mono-dev] Why does .NET object lifetime not extend into an instance method call?<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Fri, Aug 24, 2012 at 10:50 AM, Jonathan Pryor <<a href="mailto:jonpryor@vt.edu" target="_blank">jonpryor@vt.edu</a>> wrote:<o:p></o:p></p>
<div>
<p class="MsoNormal">> 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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">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.<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">... snip ... <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">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.<o:p></o:p></p>
</blockquote>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">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..<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">class</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Foo</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">:</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">IDisposable</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">  
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">ManagedObject</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
 mo </span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">=</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">new</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">ManagedObject</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">();</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p> </o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">  
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">~</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Foo</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">()</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">this</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">.</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Dispose</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">();</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">}</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">  
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">public</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">void</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Dispose</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">()</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">      
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">if</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">(</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">mo
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">!=</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">null</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">)</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">         
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">try</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">mo</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">.</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Dispose</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">();}</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">finally</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
 mo </span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">=</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">null</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">;</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">}</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">      
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">}</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">  
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">}</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
<o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p> </o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">  
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">void</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Problem</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">()</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
 mo</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">.</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">doSomething</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">();</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">}</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p> </o:p></span></span></p>
<p class="MsoNormal"><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">  
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">static</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">void</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Main</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">()</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">{</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="kwd"><span style="font-family:"Arial","sans-serif";color:#000088;border:none windowtext 1.0pt;padding:0in;background:white">new</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Foo</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">().</span></span><span class="typ"><span style="font-family:"Arial","sans-serif";color:#660066;border:none windowtext 1.0pt;padding:0in;background:white">Problem</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">();</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white">
</span></span><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">}</span></span><span class="pln"><span style="font-family:"Arial","sans-serif";border:none windowtext 1.0pt;padding:0in;background:white"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span class="pun"><span style="font-family:"Arial","sans-serif";color:#666600;border:none windowtext 1.0pt;padding:0in;background:white">}</span></span>
<o:p></o:p></p>
</div>
<p class="MsoNormal"><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>
 <o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">> Why isn't this considered a bug in the .NET runtime?<o:p></o:p></p>
</div>
<p class="MsoNormal">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?<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">> (2) Does the Mono GC have the same behavior?<o:p></o:p></p>
</div>
<p class="MsoNormal">Yes, because there's no other sane behavior.<o:p></o:p></p>
</blockquote>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">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><o:p></o:p></p>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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? <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</body>
</html>