Silverlight(私の個人ブログの最後のエントリ)で記述されたアプリケーションでメモリリークを検索しているときに、メモリリークを引き起こすChildWindowクラスでエラーを見つけました。
WinDbgでは、次のようになります。
以下のModalWindowは、ChildWindowの相続人です。
0:000:x86>!Dumpheap -mt 05e4f830
アドレスMTサイズ
07068c28 05e4f830 236
070ba900 05e4f830 236
合計0個のオブジェクト
統計:
MTカウントTotalSizeクラス名
05e4f830 2 472 MLOY.MKNA.KarjaKompassi.ModalWindow
合計2個のオブジェクト
0:000:x86>!Gcroot 07068c28
注:スタックで見つかったルートは、誤検知である可能性があります。 「!Help gcroot」を実行します
詳細情報。
スキャンスレッド5 OSTHread 1880
スキャンスレッド22 OSTHread 1034
スキャンスレッド23 OSTHread 1878
スキャンスレッド24 OSTHread 1f28
ドメイン(062239E0):ハンドル(ピン留め):3cc12f8:ルート:07de4260(System.Object [])->
06ed1460(MLOY.MKNA.KarjaKompassi.FeedingPlan。Shell)->
06ed14dc(MS.Internal.CoreTypeEventHelper)->
06ef0c54(System.Collections.Generic.Dictionary`2 [[System.Int32、mscorlib]、[MS.Internal.CoreTypeEventHelper + EventAndDelegate、System.Windows]])->
070ee840(System.Collections.Generic.Dictionary`2 +エントリ[[System.Int32、mscorlib]、[MS.Internal.CoreTypeEventHelper + EventAndDelegate、System.Windows]] [])->
070a68a8(MS.Internal.CoreTypeEventHelper + EventAndDelegate)->
070a6870(System.Windows。RoutedEventHandler)->
07068c28(MLOY.MKNA.KarjaKompassi。ModalWindow)
コードでは、次のようになります(ReflectorはChildWindowクラスのSystem.Windows.Controls.dllを確認しました)。
private void ChildWindow_LostFocus(オブジェクト送信者、RoutedEventArgs e)
{
...
Application.Current.RootVisual.GotFocus + = new RoutedEventHandler(this.RootVisual_GotFocus);
...
}
public void閉じる()
{
...
Application.Current.RootVisual.GotFocus-= new RoutedEventHandler(this.RootVisual_GotFocus);
...
}
private void SubscribeToEvents()
{
base.LostFocus + = new RoutedEventHandler(this.ChildWindow_LostFocus);
}
数回サブスクライブできます(ウィンドウがフォーカスを失うたびに)が、Close()を呼び出すときに一度だけサブスクライブを解除します
解決策:
ChildWindow_LostFocusメソッドで、問題の文字列をいくつかに置き換えます。
Application.Current.RootVisual.GotFocus-= new RoutedEventHandler(this.RootVisual_GotFocus);
Application.Current.RootVisual.GotFocus + = new RoutedEventHandler(this.RootVisual_GotFocus);