Application.ThreadException:如果不分离,内存泄漏?
Application.ThreadException 的参考页说
因为这是一个静态事件,所以您必须分离事件处理程序 当您的应用程序被处置时,否则会导致内存泄漏。
尽管该页面上的示例代码没有分离事件处理程序,但如果事件处理程序没有分离,它真的会泄漏吗?
似乎唯一应该分离处理程序的时间是应用程序关闭时。在这种情况下,无论处理程序是否分离,应用程序使用的所有内存都会被释放?
The reference page for Application.ThreadException says
Because this is a static event, you must detach your event handlers
when your application is disposed, or memory leaks will result.
Notwithstanding the fact that the sample code on that very page does not detach the event handler, does it really leak if the event handler is not detached?
It seems the only time the handler should be detached is if the application shuts down. In that case, whether or not the handler is detached, all the memory used by the application will be freed anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能非常不常见,但由于某种原因,WinForms 应用程序的
Main()
方法可能如下所示:当启动屏幕关闭时,主窗口将出现,除非启动屏幕设置了
AbortStatup
属性设置为true
。如果您从初始屏幕内向Application.ThreadException
添加了一个事件处理程序,则CancelableSplashScreen
的实例将不会被垃圾回收,直到应用程序终止,这可能是相当大的。时间稍后。It is probably very uncommon, but a WinForms application's
Main()
method could possibly, for some reason, look like this:When the splash screen closes, the main window will appear, unless the splash screen set the
AbortStatup
property totrue
. If you added an event handler toApplication.ThreadException
from within the splash screen, the instance ofCancelableSplashScreen
won't be garbage collected until the application terminates, which may be a considerable time later.如果您放弃对对象的引用(假设它是一个实例方法,即事件处理程序),那么是的,将会出现泄漏;您将无法取消订阅该事件(因为您不再拥有该实例),并且该对象将一直存在,直到应用程序域结束(因为这是静态变量的生命周期)。
If you let the reference to the object go (assuming it's an instance method that is the event handler) then yes, there will be a leak; you won't be able to unsubscribe from the event (since you don't have the instance anymore) and the object will exist until the app domain ends (as that is the lifetime of static variables).