2010-01-29 11 views
7

मैंने ऐसी स्थिति में भाग लिया है जहां माउस मेरे WPF ऐप पर स्थानांतरित होने पर अप्रबंधित स्मृति लीक कर रहा है। विशेष रूप से, जब मैं perfmon या रेड गेट के मेमोरी प्रोफाइलर में एप्लिकेशन को प्रोफाइल करता हूं, तो निजी बाइट्स एकान्त रूप से बढ़ते हैं, लेकिन सभी प्रबंधित ढेर में बाइट स्थिर रहते हैं - जिसका मेरा मानना ​​है कि ऐप में एक अप्रबंधित रिसाव है।एक छोटे से WPF अनुप्रयोग में अप्रबंधित रिसाव

मैंने एक छोटा रेपो ऐप बनाया है, लेकिन मैं नहीं देख सकता कि समस्या कहां है।

ऐप में चार आइटमों के साथ एक सूची दृश्य शामिल है। इन वस्तुओं पर तेजी से माउस को स्थानांतरित करने से समस्या आती है।

यदि आप इस मुद्दे को पुन: उत्पन्न करने में रुचि रखते हैं तो यह कोड है - यह सुंदर नहीं है, लेकिन यह आसान है।

धन्यवाद


संपादित करें: मैं इस समस्या के लिए एक 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; 
     } 
    } 
} 

उत्तर

1

यह समस्या VisualBrush के साथ कुछ करने के लिए प्रतीत होती है जिसका उपयोग चेकर्ड पृष्ठभूमि के लिए किया जा रहा था। इसे SolidColorBrush के साथ बदलें, और समस्या दूर हो जाती है। लेकिन यह वास्तव में कोई फर्क नहीं पड़ता: माइक्रोसॉफ्ट के लोगों ने सुझाव दिया कि मैं .NETFx 3.5sp1 पर नवीनतम अपग्रेड स्थापित करता हूं, और यह समस्या को ठीक करने लगता है (अधिक जानकारी here)।

6

आप WPF बाइंडिंग में एक ज्ञात स्मृति रिसाव है कि हो सकता है जब वर्गों के गुणों के लिए नहीं बाध्यकारी बनाए गए INotifyPropertyChanged लागू करना। आपके मामले में NamePicture वर्ग की संपत्ति।

समाधान एक-बार के लिए बाध्य मोड सेट कर रहे हैं, Picture लागू INotifyPropertyChanged दे या एक निर्भरता संपत्ति में Name बना रही है।

ms support और this blog post से इसके बारे में और पढ़ें। ये और अन्य ज्ञात WPF मेमोरी लीक here मिल सकते हैं।

+0

मुझे नहीं लगता कि यह समस्या है। 'चित्र' 'INotifyPropertyChanged' को लागू करने के लिए उपयोग किया जाता है, लेकिन मैंने इसे अपने रेपो ऐप को छोटा करने के लिए हटा दिया।इसके अलावा, अगर यह मुद्दा था, तो मुझे विश्वास है कि रिसाव प्रबंधित किया जाएगा - डेटा-बाध्यकारी प्रणाली संदर्भित वस्तुओं को संदर्भित रखेगी जो इसे नहीं करना चाहिए। डब्ल्यूपीएफ लीक की सूची एक अच्छी है (और मैं सभी को इसकी जांच करने की सलाह देता हूं), लेकिन मैंने उस सूची की समीक्षा की, और मुझे लगता है कि केवल # 2 लागू होता है और यही वह मुद्दा है जिसका आपने उल्लेख किया है, जो समस्या नहीं प्रतीत होता है यहाँ। मैं ईमानदारी से सोचता हूं कि यह मुद्दा एक नया है। –

संबंधित मुद्दे