在 ItemsControl 中使用 TextBox 时发生内存泄漏

Memory leak when using a TextBox within an ItemsControl

提问人:rukiman 提问时间:11/14/2023 最后编辑:Sir Ruforukiman 更新时间:11/14/2023 访问量:77

问:

我一直在努力寻找内存泄漏的原因。我有以下 XAML:

                <ItemsControl Grid.Row="0" Grid.Column="1" x:Name="FieldsView" Background="Transparent">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="0 5">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MaxWidth="300" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <StackPanel Grid.Column="1">
                                <Grid>
                                    <TextBox Tag="firstname" Text="" /> 
                                </Grid>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

我删除了所有绑定以简化 XAML。我正在注释一些东西以找到核心泄漏。

根据 dotMemory 的说法,这是泄漏分析

enter image description here

我知道它是文本框,因为如果我注释掉它,泄漏就会消失。我正在使用 .NET Framework 4.7.2。

我看到一篇关于撤消功能的帖子,所以我设置了我的文本框的样式以删除它,但这并没有解决它。

    <Style TargetType="{x:Type TextBox}">
    <Setter Property="IsUndoEnabled" Value="False"/>
    <Setter Property="Background" Value="{StaticResource TextBox.Background}" />
    <Setter Property="Opacity" Value="60" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="FontSize" Value="22pt"/>
    <Setter Property="FontFamily" Value="Segoe UI Light" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="BorderBrush" Value="#FF7EB4EA" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

ItemsControl 是使用 ObservableCollection 填充的

private ObservableCollection<RegistrationFieldViewModel> _model;
_model = new ObservableCollection<RegistrationFieldViewModel>(visitorTypeSettingInfo.Fields.Select(a => new RegistrationFieldViewModel
        {
            Key = a.Key,
            Mandatory = a.Mandatory,
            UseAsLookup = a.UseAsLookup,
            Type = a.Type
        }).ToList());
        FieldsView.ItemsSource = _model;

和 RegistrationFieldViewModel 实现 INotifyPropertyChanged

C# WPF XAML

评论

0赞 Etienne de Martel 11/14/2023
你怎么认为这是泄漏?
0赞 rukiman 11/14/2023
因为每次我在应用程序中执行工作流时,都会保留对 UserControl 的引用。我使用 dotMemory 拍摄快照,执行一个工作流,然后执行另一个快照,然后比较两者。我还在每个工作流之后强制执行 GC 集合。
0赞 NPras 11/14/2023
在过多关注 之前,您是否尝试过使用其他控件(例如或 )并查看它们的行为方式?另外,您是否在连接调试器的情况下进行分析?(众所周知,VS 调试器保留引用的时间比它需要的时间长得多)TextBoxLabelTextBlock
0赞 rukiman 11/14/2023
好吧,我认为你是对的......必须与运行调试器有关。在这上面浪费了太多时间,把我的头发拔掉了。谢谢。
1赞 NPras 11/14/2023
这是每个开发人员的成年礼。学习在调试器打开时不信任任何内存/性能配置文件的痛苦方法。

答: 暂无答案