0

メモリ リークのある Visual Studio 2012 プロジェクト

こんにちは!MVVM Light Toolkit でインタラクション トリガーを使用すると、メモリ リークが発生することがわかりました。私はこのxamlを使用します

<UserControl x:Class="MemoryLeakTest.MainPage"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:ignore="http://www.ignore.com"
         mc:Ignorable="d ignore"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            xmlns:mvvm="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL5"
         x:Name="control"
         DataContext="{Binding Main, Source={StaticResource Locator}}">

<Grid x:Name="LayoutRoot">

    <ItemsControl ItemsSource="{Binding LeakObjects}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border  Width="300" BorderThickness="6" BorderBrush="BlueViolet" CornerRadius="3">
                    <Grid Background="{Binding ColorBrush}" >
                        <StackPanel>
                            <Button Command="{Binding ElementName=control, Path=DataContext.Command}"  Width="100" Height="40" Content="Tryck!">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <mvvm:EventToCommand Command="{Binding ElementName=control, Path=DataContext.Command}" CommandParameter="{Binding}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Button>
                            <TextBlock Text="{Binding Text}"/>
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

次に、リスト LeakObjects を再バインドして、新しいアイテムを作成します。ボタンやテキスト ブロックなどの古いアイテム (xaml) はまだメモリ内にあり、GC は実行されません。

私が書くなら

<Button Command="{Binding ElementName=control, Path=DataContext.Command}"  Width="100" Height="40" Content="Press!"/>

ボタン コマンド パラメータを使用すると、メモリ リークは発生しませんが、

<i:Interaction.Triggers>
     <i:EventTrigger EventName="Click">
          <mvvm:EventToCommand Command="{Binding ElementName=control, Path=DataContext.Command}" CommandParameter="{Binding}"/>
     </i:EventTrigger>
 </i:Interaction.Triggers>

大きな漏れがあります。

問題は、グリッドなどにコマンド パラメータがないことです。

リンクのプロジェクトには、この問題を示す非常に単純なプロジェクトがあります。

メモリリークを回避する方法はありますか? 使い方が悪いのかな。

このメモリ リークはアプリケーション全体に及ぶため、これを修正する方法を見つけることが重要です。

4

1 に答える 1

1

私もこの漏れを経験しました。EventToCommands を使用せず、単純なイベント ハンドラーを使用して、ページのコード ビハインドでこれらのメソッドからコマンドを呼び出すことで解決しました。それほどきれいではありませんが、機能し、ページは期待どおりガベージコレクションされています。または、代わりにInvokeCommandAction
を使用することもできます。http://www.dotnetpatterns.net/entries/21-Memory-Leak-issue-with-EventToCommand-passing-binding-to-RelayCommand

于 2013-06-18T08:50:24.960 に答える