Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:41:03+00:00 2026-06-12T15:41:03+00:00

I currently have a strange memory leak with WPF TreeView. When I select an

  • 0

I currently have a strange memory leak with WPF TreeView. When I select an item in the TreeView, the corresponding bound ViewModel is strongely hold in the TreeView EffectiveValueEntry[] collection. The issue is that it is not released when the ViewModel is removed from it’s parent collection.

Here is a simple code to reproduce the issue :

MainWindow.xaml

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace TreeViewMemoryLeak
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public ObservableCollection<Entry> Entries
        {
            get
            {
                if (entries == null)
                {
                    entries = new ObservableCollection<Entry>() { new Entry() { DisplayName = "First Entry" } };
                }
                return entries;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e) { entries.Clear(); }

        private ObservableCollection<Entry> entries;

    }

    public class Entry : DependencyObject
    {
        public string DisplayName { get; set; }
    }
}

MainWindow.xaml.cs

<Window x:Class="TreeViewMemoryLeak.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TreeViewMemoryLeak"
    Title="MainWindow" Height="350" Width="250">

    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Entry}">
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </Window.Resources>

    <StackPanel>
        <Button Content="delete item" Click="Button_Click" Grid.Row="0" Margin="10"/>
        <TreeView x:Name="treeView" ItemsSource="{Binding Entries}" Grid.Row="1" Margin="10" BorderBrush="Black" BorderThickness="1" />
    </StackPanel>

</Window>

To reproduce the issue

Select the item, then click the button to clear the ObservableCollection. Now check the EffectiveValueEntry[] on the TreeView control : the ViewModel is still there and is not flagged for garbage collection.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:41:04+00:00Added an answer on June 12, 2026 at 3:41 pm

    Well I finally came up with a rather violent solution. I remove the reference from the EffectiveValues collection myself when deleting the last object in the TreeView. It may be overkill but at least, it works.

    public class MyTreeView : TreeView
    {
        protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
        {
            base.OnSelectedItemChanged(e);
    
            if (Items.Count == 0)
            {
                var lastObjectDeleted = e.OldValue;
                if (lastObjectDeleted != null)
                {
                    var effectiveValues = EffectiveValuesGetMethod.Invoke(this, null) as Array;
                    if (effectiveValues == null)
                        throw new InvalidOperationException();
    
                    bool foundEntry = false;
                    int index = 0;
                    foreach (var effectiveValueEntry in effectiveValues)
                    {
                        var value = EffectiveValueEntryValueGetMethod.Invoke(effectiveValueEntry, null);
                        if (value == lastObjectDeleted)
                        {
                            foundEntry = true;
                            break;
                        }
                        index++;
                    }
    
                    if (foundEntry)
                    {
                        effectiveValues.SetValue(null, index);
                    }
                }
            }
        }
    
        protected MethodInfo EffectiveValueEntryValueGetMethod
        {
            get
            {
                if (effectiveValueEntryValueGetMethod == null)
                {
                    var effectiveValueEntryType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(t => t.Name == "EffectiveValueEntry").FirstOrDefault();
                    if (effectiveValueEntryType == null)
                        throw new InvalidOperationException();
    
                    var effectiveValueEntryValuePropertyInfo = effectiveValueEntryType.GetProperty("Value", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance);
                    if (effectiveValueEntryValuePropertyInfo == null)
                        throw new InvalidOperationException();
    
                    effectiveValueEntryValueGetMethod = effectiveValueEntryValuePropertyInfo.GetGetMethod(nonPublic: true);
                    if (effectiveValueEntryValueGetMethod == null)
                        throw new InvalidOperationException();
    
                }
                return effectiveValueEntryValueGetMethod;
            }
        }
    
        protected MethodInfo EffectiveValuesGetMethod
        {
            get
            {
                if (effectiveValuesGetMethod == null)
                {
                    var dependencyObjectType = typeof(DependencyObject);
                    var effectiveValuesPropertyInfo = dependencyObjectType.GetProperty("EffectiveValues", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance);
                    if (effectiveValuesPropertyInfo == null)
                        throw new InvalidOperationException();
    
                    effectiveValuesGetMethod = effectiveValuesPropertyInfo.GetGetMethod(nonPublic: true);
                    if (effectiveValuesGetMethod == null)
                        throw new InvalidOperationException();
                }
                return effectiveValuesGetMethod;
            }
        }
    
        #region Private fields
        private MethodInfo effectiveValueEntryValueGetMethod;
        private MethodInfo effectiveValuesGetMethod;
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently i have a strange behavior in my WPF Application. I have a Main
I have a strange issue . I am currently working on a mail app
I'm currently writing a Rails app, and hit a somewhat strange quirk. I have
Alright, I have a strange one here. We currently have a tools application that
I'm currently developing an responsive website. I have a very strange problem in Google
Hello I am currently working with sockets and input/output streams. I have a strange
I have some strange comportement with my ajax request. The ajax request processes correctly,
Currently have a drop down menu that is activated on a hover (from display:none
Currently have password protection on my main and sub directories, however I'd like to
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.