gpt4 book ai didi

c# - 普通 WPF 应用程序中的非托管泄漏

转载 作者:太空狗 更新时间:2023-10-29 20:15:20 31 4
gpt4 key购买 nike

我遇到过这样一种情况:当鼠标移到我的 WPF 应用程序上时,我会泄漏非托管内存。具体来说,当我在 perfmon 或 Red Gate 的内存分析器中分析应用程序时,私有(private)字节单调增加,但所有托管堆中的字节保持不变——我相信,这意味着应用程序有一个未管理的泄漏。

我创建了一个普通的重现应用程序,但我看不出问题出在哪里。

该应用程序由一个包含四个项目的 ListView 组成。将鼠标快速移动到这些项目上会导致问题。

如果您有兴趣重现该问题,请查看这里的代码——它不漂亮,但很简单。

谢谢


编辑:我创建了一个 Microsoft Connect issue对于这个问题。


App.xaml

<Application x:Class="WpfLeakRepro.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

App.xaml.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace WpfLeakRepro
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush x:Key="ListItemHover"
EndPoint="0,1"
StartPoint="0,0">
<GradientStop Color="Aqua"
Offset="0" />
<GradientStop Color="White"
Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="ListItemSelected"
EndPoint="0,1"
StartPoint="0,0">
<GradientStop Color="Blue"
Offset="0" />
<GradientStop Color="White"
Offset="1" />
</LinearGradientBrush>
<VisualBrush x:Key="CheckeredBackground"
Viewport="20,20,20,20"
ViewportUnits="Absolute"
TileMode="Tile"
Stretch="Fill">
<VisualBrush.Visual>
<Canvas Opacity="5">
<Rectangle Fill="#FF606060"
Height="21"
Width="21"
Canvas.Top="20" />
<Rectangle Fill="#FF606060"
Width="21"
Height="21"
Canvas.Left="20" />
<Rectangle Fill="#FF646464"
Height="21"
Width="21"
Canvas.Left="20"
Canvas.Top="20" />
<Rectangle Fill="#FF646464"
Width="21"
Height="21" />
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border Background="{TemplateBinding Background}">
<Grid>
<GridViewRowPresenter />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Background"
Value="{DynamicResource ListItemHover}" />
</Trigger>
<Trigger Property="IsSelected"
Value="true">
<Setter Property="Background"
Value="{DynamicResource ListItemSelected}" />
</Trigger>
</ControlTemplate.Triggers>

</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

Window1.xaml

<Window x:Class="WpfLeakRepro.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="449" Width="497">
<Grid>
<ListView Margin="12"
Name="listView"
ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="File Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Thumbnail">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border Background="{DynamicResource CheckeredBackground}" Width="72" Height=48/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>

Window1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.IO;

namespace WpfLeakRepro
{
public class Picture
{
public string Name { get; set; }
}

/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

List<Picture> pictures = new List<Picture>();

string[] images = new string[] {"Blue hills.jpg", "Sunset.jpg", "Water lilies.jpg", "Winter.jpg" };

foreach (string imagePath in images)
{
pictures.Add(new Picture() { Name = imagePath });
}

DataContext = pictures;
}
}
}

最佳答案

您可能已经在 WPF 绑定(bind)中创建了一个已知的内存泄漏,当绑定(bind)到未实现 INotifyPropertyChanged 的类的属性时可能会发生这种情况。在您的例子中,Picture 类的 Name 属性。

解决方案是将绑定(bind)模式设置为 OneTime,让 Picture 实现 INotifyPropertyChanged 或使 Name 成为依赖属性。

ms support 了解更多信息和 this blog post .可以找到这些和其他已知的 WPF 内存泄漏 here .

关于c# - 普通 WPF 应用程序中的非托管泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2165448/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com