c# - DependencyProperty内存泄漏

标签 c# wpf c#-4.0

我正在使用 PerfView 来发现内存泄漏。

比较两个快照后,我注意到在 PerView 中的 RefTree -> static vars 选项卡下。 MyPageDependencyProperty我的占 Inc% 的 78.9%。

MyPageDependencyProperty不应该在那里,因为我关闭了它所属的 xaml 窗口。

我不使用 AddValueChanged这可能会导致内存泄漏。

DependencyProperty揭示 ObservableCollection<object> .

有谁知道我可以解决这个问题吗?

谢谢

最佳答案

与其像这样定义您的属性,不如创建一个静态 ObservableCollection。

public static readonly DependencyProperty SomePropertyProperty  = DependencyProperty.Register(typeof(..), typeof(..),....,.... , new ObservableCollection<...>());

你应该这样做:

public static readonly DependencyProperty SomePropertyProperty = DependencyProperty.Register(typeof(..), typeof(..),...., null);

public MyControl()
{
   this.SomeProperty = new ObservableCollection<...>();
}

您的问题会神奇地消失。 :)

关于c# - DependencyProperty内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974061/

相关文章:

C# GraphQLRequest : Variable $max is used by but not declared

c# - 控制台选择停止应用程序

c# - 在 ASP.NET 中重写 URL?

wpf - WPF:这种类型的CollectionView不支持更改错误

wpf - 通过绑定(bind)启用 TabItem

c# - 如何在单行中将逗号转换为句号,将句号转换为逗号

c# - 输入数字或 Q 退出

.net - 在 dotnet 中使用注释

c# - 首先使用实体​​框架代码为存储库实现通用方法

c# - WPF:如何高效地每秒更新图像 30 次