-2

I am hoping to have a generic (extension) method which takes parent control and traverse through to its all children controls and unsubscribe their events if any.

problem i am closely looking at memory consumption when i create some wpf based form, memory spike which is expected considering the number of ui elements and their events but when form closes i expect to memory get release as in wpf, i m assuming form get disposed automatically disposed once closed so GC should clean up and release the memory... but thats what not happening as i waited few minutes and looked on private memory usage in diagnostic tools, that remains same. so i m wondering its not completely disposing/unsubscribing events etc

Muhammad Adnan
  • 1,375
  • 6
  • 19
  • 40
  • You can certainly do that. But the question is: Why do you need that? Please show us what you have tried so far. – haindl Oct 07 '16 at 23:34
  • i am closely looking at memory consumption when i create some wpf based form, memory spike which is expected considering the number of ui elements and their events but when form closes i expect to memory get release as in wpf, i m assuming form get disposed automatically disposed once closed so GC should clean up and release the memory... but thats what not happening as i waited few minutes and looked on private memory usage in diagnostic tools, that remains same. so i m wondering its not completely disposing/unsubscribing events etc. – Muhammad Adnan Oct 08 '16 at 00:06
  • You should take some memory snapshots in Visual Studio (or with the help of other tools) and look at the root references of the instances that you expect to get GCed. In my experience event handlers of WPF UI elements usually are not responsible for not GCing these elements. A common cause for memory leaks is (for example) the wrong usage of ResourceDictionaries. Often they are merged many times at different places, but that causes a lot of unnecessary instances. Ideally they are merged only one time in the Application resources. But there could be many other causes as well. Look at the roots! – haindl Oct 08 '16 at 00:28

1 Answers1

0

I can't speak to whether or not having many subscribed events negatively affects performance, but I can certainly answer your other two questions:

  1. You can use the following extension to enumerate all children:

    public static IEnumerable<T> GetVisualChildren<T>(this DependencyObject Parent) where T : DependencyObject
    {
        if (Parent != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(Parent); i++)
            {
                var Child = VisualTreeHelper.GetChild(Parent, i);
                if (Child != null && Child is T)
                    yield return (T)Child;
                foreach (var ChildOfChild in GetVisualChildren<T>(Child))
                    yield return ChildOfChild;
            }
        }
    }
    
    foreach (myControlInstance.GetVisualChildren<DependencyObject>())
    {
        //Unsubscribe events
    }
    
  2. To unsubscribe all events from the object, refer to this SO article:

How do I unsubscribe all handlers from an event for a particular class in C#?

Community
  • 1
  • 1