Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
95
Garbage Collection Issue
posted

Hello,

I'm having a garbage collection issue, and the only thing holding a reference to the object I want to collect is RibbonTabItem. Hoping someone can provide some insight to point me in the right direction.

Some quick background: The application I'm working on uses a RibbonWindow. The main content area is bound to a CurrentActivityWorkspace property. Most buttons in the ribbon above the content area cause the CurrentActivityWorkspace property to be set to a particular view-model. DataTemplates then allow WPF to render the view (UserControl) for that view-model. I also have a mechanism in place to assocate the UserControl with a particular context-specific RibbonTabItem and add to RibbonMenu.ContextualTabGroups at the same time. The issue I'm having is that, according to ANTS memory profiler, my UserControl is not being garbage collected after I close it.

There were several objects maintaining strong references to my UserControl, and I have been able to eliminate all of them except for one. This last reference is coming from Infragistics.Windows.Ribbon.RibbonTabItem._effectiveValues. I think if I can eliminate this last strong reference, my UserControl will be collected by the GC.

I don't see any direct references from my RibbonTabItem XAML to my UserControl. It's being given some Resources and some RibbonGroups. Each RibbonGroup has some ButtonTools which are bound to ICommands. When the ICommand is executed, CurrentActivityWorkspace is set.

As a test, I added code that calls RibbonMenu.Tabs.Clear() and RibbonMenu.ContextualTabGroups.Clear() before taking my memory snapshot. The strong reference remains.

So my two questions are:

  1. Can you think of a reason why RibbonTabItem's private _effectiveValues field might be referencing my UserControl? 
  2. Can you think of a reason why the RibbonTabItem is even in memory anymore, now that I've cleared the tabs out of the ribbon?

Regarding #2, I came across an interesting discussion in the xamChart forum: http://forums.infragistics.com/forums/p/9722/243091.aspx#243091. The reference chain in that post includes ResourceDictionary._ownerFEs, and that issue was apparently resolved in an update to the xamChart. Similarly, every reference chain I see pointing to my RibbonTabItem also includes ResourceDictionary._ownerFEs. Perhaps there's a problem somewhere in my ResourceDictionaries?

 

Thanks!

-Andrew

  • 95
    posted

    The only thing I could think of is the DataContext property of the RibbonTabItem is set to point to my UserControl. I have code that does this at runtime. So I thought maybe I need to set DataContext to null before removing the RibbonTabItem. That worked!

    But this question still remains: if I remove the RibbonTabItem from the ribbon, shouldn't that break the reference chain and cause the GC to collect both the RibbonTabItem and the UserControl?