BeginInvoke内存泄漏? [英] BeginInvoke Memory Leak?

查看:187
本文介绍了BeginInvoke内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在排除一个线程化应用程序的一些奇怪的行为,并且偶然发现一些委托函数使用 BeginInvoke(Deligate)调用来启动Async任务。我的研究表明,使用 BeginInvoke 而不配对 EndInvoke 有机会导致内存泄漏,特别是在例外情况下抛出我相信是这种情况。



我已经给出的代码调用一个委托,并有一个回调函数调用一个新的委托本身。我最好奇的是如何执行回调。是否有必要在最后一行发布新的代表?那个评论对我来说似乎没有什么意义。此外,回调的委托调用永远不会发生一个导致泄漏的 EndInvoke 调用,是否有一个更简单的方法来清理它,然后创建回调方法?



另一个问题虽然不太重要,但功能的名称为Synchronized。从我所看到的,在方法调用中没有强制同步。 VB.NET是否自动设置同步或是最后一个开发者只是扔出来的声音?



初始调用:

  mDelegateUpdate3.BeginInvoke(mCallbackUpdate3,mDelegateUpdate3)

回调方法:

  Public Sub OnUpdateSchedule3Complete(ByVal ar As IAsyncResult)
'清理原始线程
Dim del As OPCConnectionWorkerDelegate
del = CType(ar.AsyncState,OPCConnectionWorkerDelegate)
del.EndInvoke(ar)

'我们在错误的线程上,所以我们需要切换回到UI线程
Dim ar1 As IAsyncResult
ar1 = Me.BeginInvoke(New SynchronizedScheduleCompletedDelegate(AddressOf Me.SynchronizedScheduleCompleted))
End Sub

SynchronizedScheduleCompleted方法:

  Private Sub SynchronizedScheduleCompleted()
mAttemptingUpdate = False
mOPCConnectionWorker.clearInvolked()
SetInProgress(False)
End Sub


解决方案

正确;不要调用 EndInvoke()将创建一个内存泄漏。



如果你只是想 一个异步操作,而不是处理其结果,您应该简单地调用 ThreadPool.QueueUserWorkItem(),这样就不会有内存泄漏的可能。



如果您关心结果,您应该使用任务类,这是更容易使用的 。 p>

不再有任何理由调用 Delegate.BeginInvoke()



而且,代表和方法的名称中的 Synchronized 这个词是无意义的。


I'm troubleshooting some weird behavior of a threaded application and have stumbled upon a few delegate functions that use the BeginInvoke(Deligate) call to start Async tasks. My research shows that using BeginInvoke without pairing to EndInvoke has the opportunity to cause Memory Leaks especially in the cases of Exceptions being thrown which I believe to be the case here.

The code I've been given calls a delegate and has a callback function that calls a new delegate itself. What I'm most curious with is how the callback is executed. Is it in any way necessary to issue a new delegate on the last line? That comment doesn't seem to make a lick of sense to me. Also, the callback's delegate call never issues an EndInvoke call causing leaks, is there a simpler method to cleaning this up then creating a callback method?

Another concern although less important is the name of the function as Synchronized. From what I see there is nothing forcing synchronization in the method call. Does VB.NET set up synchronization automatically or was the last developer just throwing words in to sound fancy?

The initial call:

mDelegateUpdate3.BeginInvoke(mCallbackUpdate3, mDelegateUpdate3)

The callback method:

Public Sub OnUpdateSchedule3Complete(ByVal ar As IAsyncResult)
        ' Clean up original thread
        Dim del As OPCConnectionWorkerDelegate
        del = CType(ar.AsyncState, OPCConnectionWorkerDelegate)
        del.EndInvoke(ar)

        'We are on the wrong thread so we need to switch back to the UI thread
        Dim ar1 As IAsyncResult
        ar1 = Me.BeginInvoke(New SynchronizedScheduleCompletedDelegate(AddressOf Me.SynchronizedScheduleCompleted))
End Sub

The SynchronizedScheduleCompleted Method:

Private Sub SynchronizedScheduleCompleted()
    mAttemptingUpdate = False
    mOPCConnectionWorker.clearInvolked()
    SetInProgress(False)
End Sub

解决方案

Correct; not calling EndInvoke() will create a memory leak.

If you just want to "fire-and-forget" an asynchronous operation and not handle its result, you should simply call ThreadPool.QueueUserWorkItem(), which has no potential for memory leaks.

If you do care about the result, you should use the Task class, which is much easier to use.

There is no longer any reason to call Delegate.BeginInvoke().

And, the word Synchronized in the names of the delegate and the method is meaningless.

这篇关于BeginInvoke内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆