Leaking WPF charts

May 4, 2010 at 3:12 PM

I've written an application making use of WPF Toolkit charts.  I've got a timer that updates the charts every so often.  After my app runs for a while I noticed that it's consuming a bunch of memory.  After taking a couple of process dumps (via adplus.vbs) I believe the problem is that all of my charts which I think I'm no longer using are still being referenced.  Maybe I'm doing something wrong and not releasing them correctly, but my initial guess would be that I am releasing them correct.  Basically I've got a content control and when my periodic timer goes off I create a new chart and set my content's control Content property to the new chart.  This is all that I should need to do, correct?  Below is a code snippet:

.
.
.
Chart chart = new Chart();
chart.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
chart.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
this.Content = chart;
.
.
.

Here is an output from !gcroot indicating what's holding onto the no longer needed charts:

0:000> !gcroot -nostacks 01786bd0
DOMAIN(0015D200):HANDLE(Strong):911150:Root:012c17c0(System.Threading._TimerCallback)->
012c1778(System.Threading.TimerCallback)->
012a7274(Framework.SystemsManagement.Instrumentation.Console.Console)->
013a52f8(Framework.SystemsManagement.Instrumentation.Console.Console+Refresh)->
0136fdf0(Framework.SystemsManagement.Instrumentation.Console.View)->
013a5f50(System.Windows.Controls.ContentPresenter)->
01c7b550(System.Windows.Controls.DataVisualization.Charting.Chart)->
01c7bb2c(System.Windows.Controls.DataVisualization.Charting.ResourceDictionaryDispenser)->
0138a768(System.Windows.Controls.DataVisualization.ResourceDictionaryCollection)->
0138a778(System.Collections.Generic.List`1[[System.Windows.ResourceDictionary, PresentationFramework]])->
0138d568(System.Object[])->
0138a7b8(System.Windows.ResourceDictionary)->
0139ce34(System.Collections.Generic.List`1[[System.Windows.FrameworkElement, PresentationFramework]])->
0178c43c(System.Object[])->
0178bd98(System.Windows.Controls.DataVisualization.Charting.LineSeries)->
01786bd0(System.Windows.Controls.DataVisualization.Charting.Chart)

Note the first chart (01c7b550) I'm guessing is that chart currently active in my UI.  That is holding a reference to a ResourceDictionaryDispenser which eventually seems to have a reference to the chart I no longer need.  What's up with this?  How do I fully get the chart I'm replacing to not be referenced by anything?

Thanks,

Nick