0

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?

Faby
  • 21
  • 6
  • Can you please explain where the memory leak happens when you use the first approach with a static method `OnSelCharChanged`? – dymanoid Mar 21 '19 at 12:23
  • I have this view which is composed by 9 identitical user controls (GenericGraphicSectionUserControl), each of them has its own DependencyProperty with the event handler attached. Supposedly I should be able to open and close the view and everytime it gets closed, the usercontrols associated should be disposed. If I open and close the view 6 times, I'll end up with 54 instances of GenericGraphicSectionUserControl unless I remove the handle on the ValueChanged event on my DP. – Faby Mar 21 '19 at 13:11
  • There is something wrong with your code. A simple `static` method (even 9 `static` methods) won't cause memory leaks. Either investigate your memory leaks with a memory profiler and find out what holds the instances, or provide a [mcve] that demonstrates your issue so we can give you an answer. – dymanoid Mar 21 '19 at 13:13
  • That's what I thought too, but the simple act of attaching an empty handler to my dp is enough to cause the issue. Apparently there have been plenty of discussions about how hooking events (particularly static events) can root your object. Anyway I'll try to set up a minimal example.. – Faby Mar 21 '19 at 13:21

0 Answers0