<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>emphess .NET &#187; GC</title>
	<atom:link href="http://www.emphess.net/tag/gc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emphess.net</link>
	<description>Freshly Draught Code</description>
	<lastBuildDate>Fri, 11 Nov 2011 11:57:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Composite WPF, Aggressive GC and dead events</title>
		<link>http://www.emphess.net/2008/11/21/composite-wpf-aggressive-gc-and-dead-events/</link>
		<comments>http://www.emphess.net/2008/11/21/composite-wpf-aggressive-gc-and-dead-events/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 11:11:43 +0000</pubDate>
		<dc:creator>Christoph Menge</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[CAL]]></category>
		<category><![CDATA[Composite WPF]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[GC]]></category>

		<guid isPermaLink="false">http://www.emphess.net/?p=42</guid>
		<description><![CDATA[Well, this is the first post after a very long pause, I am sorry but I had very little time&#8230; Anyway, I stumbled over some strange bug this morning that turned out to be related to the GC. But let&#8217;s just step back a minute: The application I currently work on is based on CompositeWPF [...]]]></description>
			<content:encoded><![CDATA[<p>Well, this is the first post after a very long pause, I am sorry but I had very little time&#8230; </p>
<p>Anyway, I stumbled over some strange bug this morning that turned out to be related to the GC. But let&#8217;s just step back a minute:</p>
<p>The application I currently work on is based on <a href="http://www.codeplex.com/CompositeWPF">CompositeWPF </a>. I added a very simple module to the application, a simple passive debugging module. I called it &#8220;InspectorModule&#8221;. The <code>InspectorPresenter </code>now listens for global modifications to the business object which is kept by the <code>BusinessEntityManager</code>:</p>
<pre class="brush: csharp">
public InspectorPresenter(IInspectorView _view,
            IEventAggregator _eventAggregator,
            IBusinessEntityManager _businessEntityManager)
{
    mView = _view;
    mEventAggregator = _eventAggregator;
    mBusinessEntityManager = _businessEntityManager;

    mEventAggregator.GetEvent&lt;BusinessInstanceInvalidatedEvent&gt;().Subscribe(ReloadInspector);
}
</pre>
<p>Now, upon each modification of the business object, <code>ReloadInspector </code>shall be called. So, starting the application and triggering the event &#8212; nothing happened. The <code>ReloadInspector </code>method is not being called. Why?</p>
<p>After some debugging, it suddenly worked &#8211; the only problem being: I didn&#8217;t actually <em>change </em>anything! </p>
<p>To make a long story, I realized the <code>CompositeWPF DelegateReference</code>class keeps a <code>WeakReference</code> to the object (the <code>InspectorPresenter</code>, in this case) when registering it via the <code>EventAggregator </code>as above.</p>
<p>Unfortunately, due to the simplicity of the <code>InspectorPresenter</code>, no instantiated object actually keeps a reference to the Presenter (apart from the <code>WeakReference </code>of the event which, by definition, does not prevent it from being finalized).</p>
<p>That&#8217;s why the object is being finalized and the event never fires. Actually, on the first call of the associated <code>Publish()</code> method, the Composite WPF-Internal <code>EventBase.PruneAndReturnStrategies()</code> will remove it from the list after it&#8217;s associated <code>Action </code>is supposedly null.</p>
<p>Debugging this was quite cumbersome, and the solution of striking simplicity: I simply continued to develop the &#8220;Inspector&#8221;, a little more than I originally planned, so the user can now select an object in the inspector. Now, I have a simple event like this:</p>
<pre class="brush: csharp">
public InspectorPresenter(IInspectorView _view,
            IEventAggregator _eventAggregator,
            IBusinessEntityManager _businessEntityManager)
{
    // ...
    mView.SelectedNodeChanged += new OnSelectedNodeChanged(mView_SelectedNodeChanged);
    // ...
}
</pre>
<p>This keeps a (strong) reference to the Presenter, and the GC does not kill my presenter anymore! The reason it worked when debugging, I guess, is that the debugger will keep the GC from finalizing objects under certain circumstances.</p>
<div class="tweetthis" style="text-align:right;"><p> <a rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Composite+WPF%2C+Aggressive+GC+and+dead+events+http%3A%2F%2Femphess.net%2F%3Fp%3D42" title="Post to Twitter"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter2.png" alt="Post to Twitter" /></a> <a rel="nofollow" class="tt" href="http://delicious.com/post?url=http://www.emphess.net/2008/11/21/composite-wpf-aggressive-gc-and-dead-events/&amp;title=Composite+WPF%2C+Aggressive+GC+and+dead+events" title="Post to Delicious"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious.png" alt="Post to Delicious" /></a> <a rel="nofollow" class="tt" href="http://digg.com/submit?url=http://www.emphess.net/2008/11/21/composite-wpf-aggressive-gc-and-dead-events/&amp;title=Composite+WPF%2C+Aggressive+GC+and+dead+events" title="Post to Digg"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/digg/tt-digg.png" alt="Post to Digg" /></a> <a rel="nofollow" class="tt" href="http://www.facebook.com/share.php?u=http://www.emphess.net/2008/11/21/composite-wpf-aggressive-gc-and-dead-events/&amp;t=Composite+WPF%2C+Aggressive+GC+and+dead+events" title="Post to Facebook"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook.png" alt="Post to Facebook" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.emphess.net/2008/11/21/composite-wpf-aggressive-gc-and-dead-events/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

