<?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>Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx</link><description>This problem actually comes up pretty often so I thought I'd write a little article about it, and a couple of approaches to solving it. Basically any time you take an object "Your Object" whose life you want the GC to manage and then create a reference</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1440899</link><pubDate>Wed, 10 Jan 2007 01:40:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1440899</guid><dc:creator>barrkel</dc:creator><description>&lt;p&gt;I still think this would be better solved with a weak delegate mechanism though.&lt;/p&gt;
</description></item><item><title>re: Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1441430</link><pubDate>Wed, 10 Jan 2007 03:18:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1441430</guid><dc:creator>Jon Arild Tørresdal</dc:creator><description>&lt;p&gt;I've commented your post here &lt;a rel="nofollow" target="_new" href="http://blog.torresdal.net/PermaLink,guid,e4b6d502-6747-47cd-9fb7-bfc00811f16c.aspx"&gt;http://blog.torresdal.net/PermaLink,guid,e4b6d502-6747-47cd-9fb7-bfc00811f16c.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1442763</link><pubDate>Wed, 10 Jan 2007 09:42:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1442763</guid><dc:creator>nzeeman</dc:creator><description>&lt;p&gt;Can anybody tell us what was the reasoning for the design behind the strong reference model used in events in the first place?&lt;/p&gt;
</description></item><item><title>re: Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1444936</link><pubDate>Wed, 10 Jan 2007 18:19:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1444936</guid><dc:creator>Ben</dc:creator><description>&lt;p&gt;[quote]Can anybody tell us what was the reasoning for the design behind the strong reference model used in events in the first place?[/quote]&lt;/p&gt;
&lt;p&gt;I don't think there was ever a direct choice, whether the event model would be strong or weak references. &amp;nbsp;Rather the event model uses delegates, and delegates use strong references.&lt;/p&gt;
&lt;p&gt;Most use cases for delegates other than event subscription require strong references (unless you'd prefer the GC cleanup all your work items leaving the thread pool to execute dangling delegates). &amp;nbsp;In fact, the strong reference is directly exposed as the Delegate.Target property. &amp;nbsp;Correct me if I'm wrong, but anonymous delegates w/binding to local parameters is fundamentally incompatible with weakly referenced delegates.&lt;/p&gt;
</description></item><item><title>re: Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1444951</link><pubDate>Wed, 10 Jan 2007 18:26:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1444951</guid><dc:creator>Ben</dc:creator><description>&lt;p&gt;Then again, if generics supported inheritance from a generic parameter, it would be almost trivial to make a flexible WeakDelegate wrapper. &amp;nbsp;As it is, I suspect a little lightweight codegen could do the trick, letting the compiler do generic argument deduction to give you the exact delegate type, which you can reflect against to determine the number and types of arguments.&lt;/p&gt;
</description></item><item><title>re: Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1446251</link><pubDate>Thu, 11 Jan 2007 01:48:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1446251</guid><dc:creator>ricom</dc:creator><description>&lt;p&gt;You're right that weak references would make anonymous delegates problematic.&lt;/p&gt;
</description></item><item><title>re: Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1449932</link><pubDate>Thu, 11 Jan 2007 12:09:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1449932</guid><dc:creator>Tess</dc:creator><description>&lt;p&gt;Here are two debugging walkthroughs for finding memory leaks caused by attaching instance eventhandlers to cached and static objects&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/tess/archive/2006/01/23/516139.aspx"&gt;http://blogs.msdn.com/tess/archive/2006/01/23/516139.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/tess/archive/2006/11/27/asp-net-crash-bad-cacheitemremovedcallback-part-ii.aspx"&gt;http://blogs.msdn.com/tess/archive/2006/11/27/asp-net-crash-bad-cacheitemremovedcallback-part-ii.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Interesting Finds: January 13, 2007 Part B</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1462226</link><pubDate>Sat, 13 Jan 2007 21:59:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1462226</guid><dc:creator>Jason Haley</dc:creator><description /></item><item><title>re: Memory leaks 101: Objects anchored by event generators</title><link>http://blogs.msdn.com/ricom/archive/2007/01/09/memory-leaks-101-objects-anchored-by-event-generators.aspx#1492904</link><pubDate>Fri, 19 Jan 2007 15:17:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1492904</guid><dc:creator>Darren Oakey</dc:creator><description>&lt;p&gt;This issue bites us every day. &amp;nbsp;We've tried to make our own weak reference methods - even dynamically creating the IL for the callbacks - but we can't get it anywhere near as fast as the event system, which is important to us :( &amp;nbsp; &lt;/p&gt;
&lt;p&gt;But basically, almost every bit of code in our system is asynchronous, so has to be hooked up using some sort of event or callback - it wouldn't make sense to completely invert the object hierarchy, so it's almost as if we have no GC - we are back to the bad old world where we must explicitly destruct (dispose) every single object at the right time - and sometimes miss one.. even a single mistake can lock a huge tree in memory.&lt;/p&gt;
&lt;p&gt;The thing is, given that you can also use a callback or handler interface, which we do for everything that absolutely has to be listened to, I don't think there has ever been a time where I have used an event and would actually want that to lock the recipient class into memory... &lt;/p&gt;
&lt;p&gt;I think mostly when we use an event we are thinking decoupling - we are thinking A references B, but B doesn't reference A.&lt;/p&gt;
&lt;p&gt;If there was any way to change the current event system over to weak referenced, we'd be soooo happy... but even a second event paradigm would be incredibly useful!&lt;/p&gt;
&lt;p&gt;Please? :)&lt;/p&gt;
&lt;p&gt;Actually, thinking of it- there are several sort of ways of doing a weak reference based event on the net - none are super efficient, but what would be useful would be if some MS person (like you :)) - went through and created the &amp;quot;ideal&amp;quot; weak referenced event pattern. &amp;nbsp;We could all start using it now, and it could just nicely flow into Orcas.&lt;/p&gt;
</description></item></channel></rss>