TheWommies
TheWommies

Reputation: 5072

WPF UserControl Memory leak

I have a UserControl in an application where I am constantly swapping the Content property with other UIElements.

However I noticed in Task Manager after a while the memory goes up after a number of swaps.

Using AntProfiler I noticed for some reason there ToolBarAutomationPeer that the UserControl references.

To fix this Memory leak I made my own UserControl with the following code

public class MyUserControl : UserControl
{
    protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
    {
        return null;        
    }
}

This seems to remove any AutomationPeer instances that the UserControl may reference that may keep the Content that I am swapping in memory...

But I am still interested to know how did a ToolBarAutomationPeer get into my UserControl, and what are the ramifications of me returning null in the OnCreateAutomationPeer method?

I am not well versed with Automation calls and not sure when they would be useful.

Upvotes: 3

Views: 3528

Answers (2)

justin.m.chase
justin.m.chase

Reputation: 13655

I would be curious to see more code to try to figure out why the ToolBarAutomationPeer is appearing also but basically the automation peers are for accessibility. Screen readers and other automation tools can use the automation peers to run your application. Common uses are for people who are handicapped in one way or another and also test automation tools.

By returning null like you are above you are making your UserControl completely inaccessible to automation.

Upvotes: 2

Shaun Rowan
Shaun Rowan

Reputation: 9539

The automation kicks in if you have an automation client running on your computer. The most common being:

  • Tablet PC Input Service (in other words, all 'tablet like' PCs)
  • Automated Testing tools
  • Screen Readers (and other accessability software)

This makes silverlight a complete mess and causes a number of bugs, and almost-always makes everything leak like crazy.

I disabled automation by setting this parameter in my html:

<param name="windowless" value="true" />

You can read more here: Silverlight + MVVM + Bindings = Memory leaks?

Upvotes: 2

Related Questions