2010-12-03 48 views
0

我正在使用以下代碼來測試我正在嘗試解決的問題。Vb.net WPF調度員委託內存泄漏幫助

Class MainWindow 
    Dim bw As BackgroundWorker = New BackgroundWorker() 

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 
     AddHandler bw.DoWork, AddressOf bw_DoWork 
     AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted 

     bw.RunWorkerAsync() 
    End Sub 

    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) 
     For i As Integer = 0 To 250000 
      Dim d As New test_delegate(AddressOf test) 
      Dispatcher.Invoke(d, " SUPER SUPER SUPERSPERSUPERSUPERvSUPERSUPERSUPERSUPERv LONG Test") 
     Next 
    End Sub 

    Private Delegate Sub test_delegate(ByVal txt As String) 
    Private Sub test(ByVal txt As String) 
     txtTest.Text = txt 
    End Sub 
End Class 

應用程序緩慢而明顯地泄漏內存。

我該如何清理(或處置)我創建的每個代表? 因爲沒有d.dispose?

謝謝!

+1

我跑這個例子,但我沒有看到任何內存泄漏。隨着時間的推移,我確實看到了一點記憶力的增加,但沒有任何跡象表明除了創造25萬名代表之外,還有其他任何東西。 – rossisdead 2010-12-03 22:59:22

回答

0

我想通了,如果你在全球範圍內宣佈新的委託,並調用它根據需要修復此問題:

Class MainWindow 
    Dim bw As BackgroundWorker = New BackgroundWorker() 

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 
     AddHandler bw.DoWork, AddressOf bw_DoWork 
     AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted 

     bw.RunWorkerAsync() 
    End Sub 

    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) 
     For i As Integer = 0 To 250000 
      Dispatcher.Invoke(d, " SUPER SUPER SUPERSPERSUPERSUPERvSUPERSUPERSUPERSUPERv LONG Test") 
     Next 
    End Sub 

    Dim d As New test_delegate(AddressOf test) 
    Private Delegate Sub test_delegate(ByVal txt As String) 
    Private Sub test(ByVal txt As String) 
     txtTest.Text = txt 
    End Sub 
End Class