<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Porting C++ native code to managed (/clr) - Threads</title><link>http://blogs.msdn.com/b/dsvc/archive/2009/02/23/porting-c-native-code-to-managed-clr-threads.aspx</link><description>In native world, one interacts with OS directly by calling Win32 APIs for managing resources (like allocating/deallocating memory,opening/closing handles). In managed world one relies on CLR totally or at times partially for doing the same( eg GC does</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Porting C++ native code to managed (/clr) - Threads</title><link>http://blogs.msdn.com/b/dsvc/archive/2009/02/23/porting-c-native-code-to-managed-clr-threads.aspx#9931807</link><pubDate>Thu, 03 Dec 2009 05:26:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9931807</guid><dc:creator>Sheila</dc:creator><description>&lt;p&gt;Finally an explanation that makes sense.&lt;/p&gt;
&lt;p&gt;I am having this exact same problem with a server application which spawns native threads for each client connection. As part of the initialization it calls a very small amount of interop C++ code, but does the rest of its work in native code. When the client disconnects the thread exits.&lt;/p&gt;
&lt;p&gt;Since the clients don&amp;#39;t typically connect and disconnect very often (it&amp;#39;s too expensive) it took a long, long time for the handle count to get big enough to look out of place. However, at some point the client scripted an application that connected and disconnected once per minute. But worse, because the server didn&amp;#39;t ever execute very much managed code, the GC never thought there was enough memory pressure to warrant any collection. &amp;nbsp;The dead threads started really building up, and the handle count kept increasing until eventually the server ran out of handles and failed catastrophically. This took weeks to happen, though.&lt;/p&gt;
&lt;p&gt;For a long time I was chasing what I thought were &amp;quot;missing&amp;quot; Dispose calls somewhere under the interop code, until eventually I realized that it was still leaking handles even if I commented out the interop code so that it didn&amp;#39;t actually do anything. The simple act of crossing from native to managed code allocated 5 handles per thread.&lt;/p&gt;
&lt;p&gt;Sounds like forcing collection is the way to go...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9931807" width="1" height="1"&gt;</description></item></channel></rss>