<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Forcing Event Consumer Cleanup without Weak Events</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/</link>
	<description>Colin Eberhardt&#039;s Adventures in .NET</description>
	<lastBuildDate>Thu, 09 Feb 2012 03:02:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Michael</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/#comment-17587</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Thu, 19 Aug 2010 15:02:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=568#comment-17587</guid>
		<description>&quot;This does not cause significant problems if the listener has a shorter or the same lifecycle that the source, as is the case where you add event handlers for controls in a Windows Form for example.&quot;

This sentence is backwards. There are not problems if the *source* has a shorter or equal lifetime as the *listener*, because it is the source which is preventing the listener from being collected.</description>
		<content:encoded><![CDATA[<p>&#8220;This does not cause significant problems if the listener has a shorter or the same lifecycle that the source, as is the case where you add event handlers for controls in a Windows Form for example.&#8221;</p>
<p>This sentence is backwards. There are not problems if the *source* has a shorter or equal lifetime as the *listener*, because it is the source which is preventing the listener from being collected.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin Eberhardt</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/#comment-17574</link>
		<dc:creator>Colin Eberhardt</dc:creator>
		<pubDate>Thu, 19 Aug 2010 09:48:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=568#comment-17574</guid>
		<description>@Ashley,

Which one are not working for you? If you let me know I can fix them :-)

Colin E.</description>
		<content:encoded><![CDATA[<p>@Ashley,</p>
<p>Which one are not working for you? If you let me know I can fix them <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Colin E.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ashley Marmolejo</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/#comment-17480</link>
		<dc:creator>Ashley Marmolejo</dc:creator>
		<pubDate>Tue, 17 Aug 2010 16:10:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=568#comment-17480</guid>
		<description>Some of your links don&#039;t seem to work.</description>
		<content:encoded><![CDATA[<p>Some of your links don&#8217;t seem to work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zach Saw</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/#comment-9378</link>
		<dc:creator>Zach Saw</dc:creator>
		<pubDate>Tue, 16 Mar 2010 01:16:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=568#comment-9378</guid>
		<description>Let me clarify this, &quot;It’s easy to make a strong ref to an object from a weaf-ref, whereas an implicit strong ref makes it hard to break&quot;

I meant it&#039;s easy making strong ref from weak ref but not the other way around.</description>
		<content:encoded><![CDATA[<p>Let me clarify this, &#8220;It’s easy to make a strong ref to an object from a weaf-ref, whereas an implicit strong ref makes it hard to break&#8221;</p>
<p>I meant it&#8217;s easy making strong ref from weak ref but not the other way around.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zach Saw</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/#comment-9377</link>
		<dc:creator>Zach Saw</dc:creator>
		<pubDate>Tue, 16 Mar 2010 01:13:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=568#comment-9377</guid>
		<description>Thanks for clarifying what you meant by &#039;dead&#039;. Reading your post, the impression I got was that you meant the object is dead in the sense of getting GC&#039;ed.

This brings me to another point.

This unpredictability is inherent to everything that uses weakref - such as Threading.Timer. Once &#039;dead&#039; (as per your definition), the timer event may still fire a few times before it gets GC&#039;ed, thus introducing unpredictability. That simply means you need to care about its lifetime and dispose it when you want it to stop. Same principle applies to weak events, you&#039;d still care about its lifetime and remove the event handlers at the correct point. For consistency, I&#039;d prefer all events be changed to weak events by default (should have been this way from the get-go). It&#039;s easy to make a strong ref to an object from a weaf-ref, whereas an implicit strong ref makes it hard to break (in fact, it&#039;ll be impossible if the event is in a third-party library where you wouldn&#039;t want to change the source / haven&#039;t got the source). The overhead to assign and call a weak event if supported natively is negligible. And with events being weak by default, developers are forced to think about the object, as opposed to the method they want triggered, when they hook it to the event.</description>
		<content:encoded><![CDATA[<p>Thanks for clarifying what you meant by &#8216;dead&#8217;. Reading your post, the impression I got was that you meant the object is dead in the sense of getting GC&#8217;ed.</p>
<p>This brings me to another point.</p>
<p>This unpredictability is inherent to everything that uses weakref &#8211; such as Threading.Timer. Once &#8216;dead&#8217; (as per your definition), the timer event may still fire a few times before it gets GC&#8217;ed, thus introducing unpredictability. That simply means you need to care about its lifetime and dispose it when you want it to stop. Same principle applies to weak events, you&#8217;d still care about its lifetime and remove the event handlers at the correct point. For consistency, I&#8217;d prefer all events be changed to weak events by default (should have been this way from the get-go). It&#8217;s easy to make a strong ref to an object from a weaf-ref, whereas an implicit strong ref makes it hard to break (in fact, it&#8217;ll be impossible if the event is in a third-party library where you wouldn&#8217;t want to change the source / haven&#8217;t got the source). The overhead to assign and call a weak event if supported natively is negligible. And with events being weak by default, developers are forced to think about the object, as opposed to the method they want triggered, when they hook it to the event.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin Eberhardt</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/#comment-9356</link>
		<dc:creator>Colin Eberhardt</dc:creator>
		<pubDate>Mon, 15 Mar 2010 09:34:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=568#comment-9356</guid>
		<description>Hi Zach, I consider an object to be &#039;dead&#039; when the programmer has removed all references to it, and allows it to be GC&#039;ed.  For example, if you have a modular UI architecture, you might create a UI module instance, a SaveDialog for example, that the user interacts with. When they have finished with it, you remove your references to it. As you indicated, the object is no longer traceable from root, i.e. there are no stack frames that have reference that reach the object, and at some point in the future it will be garbage collected.

However, if this UI module handles events from an object with a longer lifetime, the &#039;application&#039; object for example, and you do not remove the event handlers when the UI module is no longer used, a strong reference will still exist and it will not be GC&#039;ed at the point you might expect. If you use weak events, the weak reference will mean that this module will be GC&#039;ed if the only reference that exists to it is via the weak event and a memory leak no longer occurs.

However, what if the UI modules event handler performs some non-trivial logic? Perhaps as a result of handling an event from an application it writes to a database, or uses a web service? With a weak event reference this logic could be executed a number of times before the UI module is GCed. This leads to an application which behaves in an unpredictable manner.

I am not against weak events in principle. What I am against is the liberal use of them in instances where all the developer has to do is remove their events handlers at the correct point in the code. If you do so, you will never have event handlers being executed at unpredictable points in time.

Hope that helps,
Colin E.</description>
		<content:encoded><![CDATA[<p>Hi Zach, I consider an object to be &#8216;dead&#8217; when the programmer has removed all references to it, and allows it to be GC&#8217;ed.  For example, if you have a modular UI architecture, you might create a UI module instance, a SaveDialog for example, that the user interacts with. When they have finished with it, you remove your references to it. As you indicated, the object is no longer traceable from root, i.e. there are no stack frames that have reference that reach the object, and at some point in the future it will be garbage collected.</p>
<p>However, if this UI module handles events from an object with a longer lifetime, the &#8216;application&#8217; object for example, and you do not remove the event handlers when the UI module is no longer used, a strong reference will still exist and it will not be GC&#8217;ed at the point you might expect. If you use weak events, the weak reference will mean that this module will be GC&#8217;ed if the only reference that exists to it is via the weak event and a memory leak no longer occurs.</p>
<p>However, what if the UI modules event handler performs some non-trivial logic? Perhaps as a result of handling an event from an application it writes to a database, or uses a web service? With a weak event reference this logic could be executed a number of times before the UI module is GCed. This leads to an application which behaves in an unpredictable manner.</p>
<p>I am not against weak events in principle. What I am against is the liberal use of them in instances where all the developer has to do is remove their events handlers at the correct point in the code. If you do so, you will never have event handlers being executed at unpredictable points in time.</p>
<p>Hope that helps,<br />
Colin E.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zach Saw</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/#comment-9347</link>
		<dc:creator>Zach Saw</dc:creator>
		<pubDate>Mon, 15 Mar 2010 03:57:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=568#comment-9347</guid>
		<description>&quot;This code can continue to get executed long after the object itself is ‘dead’.&quot;

How so? A strong ref would need to have been made prior to the event being called. And once it&#039;s called, the object&#039;s this pointer becomes live and traceable from root. So, GC won&#039;t collect it while it&#039;s being executed.</description>
		<content:encoded><![CDATA[<p>&#8220;This code can continue to get executed long after the object itself is ‘dead’.&#8221;</p>
<p>How so? A strong ref would need to have been made prior to the event being called. And once it&#8217;s called, the object&#8217;s this pointer becomes live and traceable from root. So, GC won&#8217;t collect it while it&#8217;s being executed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin Eberhardt</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/02/forcing-event-consumer-cleanup-without-weak-events/#comment-8990</link>
		<dc:creator>Colin Eberhardt</dc:creator>
		<pubDate>Thu, 04 Mar 2010 08:09:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=568#comment-8990</guid>
		<description>Thanks Josh ... it is only a little bit of code, but I have found so many memory leaks in applications using it :-)

Colin E.</description>
		<content:encoded><![CDATA[<p>Thanks Josh &#8230; it is only a little bit of code, but I have found so many memory leaks in applications using it <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Colin E.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

