Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 771
TheWommies Avatar asked Nov 27 '25 19:11

TheWommies


1 Answers

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.

like image 60
justin.m.chase Avatar answered Nov 30 '25 10:11

justin.m.chase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!