首页 > 解决方案 > Memory leak on AddValueChanged of dependency property

问题描述

My app open and closes several times the same view and everytime it gets sluggish and sluggish as the cycle goes on. If I don't attach any handler to the ValueChanged event on my DependencyProperty, the GC collects as expected. If I add it implicitly in the declaration:

        public static DependencyProperty selectedCharProperty = DependencyProperty.Register(
        "SelectedChar",
        typeof(Characteristic),
        typeof(GenericGraphicSectionUserControl),
        new PropertyMetadata(OnSelCharChanged)
        );

or separately in the ctor:

TypeDescriptor.GetProperties(this)["SelectedChar"].AddValueChanged(this, OnSelCharChanged);

then the instances keep piling up. This is the closest question I could find on the matter but althout it compiles and everything, it doesn't seem to solve the problem. I also tried

    TypeDescriptor.GetProperties(this)["SelectedChar"].RemoveValueChanged(this, OnSelCharChanged);

set where I'm sure the control will be unloaded but it only delays the leak by one iteration. Is there anything else I could try?

标签: c#wpfmemory-leaksdependency-properties

解决方案


推荐阅读