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?
谢谢!
I am using the following code to test a problem I am trying to solve.
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
the application leaks memory slowly but noticeably.
how do I clean up (or dispose of) each delegate I am creating?
since there is no d.dispose?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现,如果您在全局范围内声明新委托并根据需要调用,它可以解决此问题:
I figured out that if you declare the new delegate in the global scope and invoke as needed it fixes this problem: