挂钩SetConsoleCtrlHandler时没有堆栈跟踪的NullReferenceException

使用代码从这个线程挂钩控制台关闭事件,我有时会得到一个没有堆栈跟踪的NullReferenceException (大多数情况下我没有)。 它发生在发布和调试中,并且“抛出exception时中断”没有帮助(它中断,但堆栈跟踪仍为空)。 当我正常退出我的应用程序时,我从未得到这个exception(这是命中输入并因此释放Console.ReadLine )。 应用程序事件日志有2个条目:

应用程序:MyApp.exe Framework版本:v4.0.30319描述:由于未处理的exception,进程已终止。 exception信息:System.NullReferenceException Stack:

和:

故障应用程序名称:Gateway.exe,版本:1.0.0.0,时间戳:0x4e284101故障模块名称:未知,版本:0.0.0.0,时间戳:0x00000000exception代码:0xc0000005故障偏移:0x004d41ce故障进程id:0xf00故障应用程序启动时间:0x01cc47b827e19a6e错误应用程序路径:C:\ dev \ MyApp.exe错误模块路径:未知报告ID:689c1caa-b3ab-11e0-ba1b-00247e777f12

谷歌已经透露了SetConsoleCtrlHandler一些错误和问题 ,所以我想知道这是否是一场失败的战斗。

像这样的代码最典型的问题是不保留对委托实例的引用。 您作为SetConsoleCtrlHandler()的第一个参数传递的那个。 垃圾收集器无法通过非托管代码查看对委托对象的引用。 因此,当垃圾收集器运行时,这最终会爆炸:

  SetConsoleCtrlHandler(Handler, true); 

这与完全相同

  SetConsoleCtrlHandler(new EventHandler(Handler), true); 

假设您使用了链接代码中的类型。 该代码的作者通过使_handler成为静态变量来仔细避免此问题。 与前两行代码创建的临时委托实例相反。 将其存储在静态变量中可确保在程序的生命周期内保持引用。 在这种特殊情况下要做的事情,因为你实际上对事件感兴趣,直到程序结束。

对于正在努力解决这个问题的vb.net,我的代码是……

 'declaration Private Declare Function SetConsoleCtrlHandler Lib "kernel32" (ByVal handlerRoutine As ConsoleEventDelegate, ByVal add As Boolean) As Boolean Public Delegate Function ConsoleEventDelegate(ByVal [event] As ConsoleEvent) As Boolean 'The close function... Public Function Application_ConsoleEvent(ByVal [event] As ConsoleEvent) As Boolean Console.WriteLine("We're closing it all down: ") Return False End Function 'creating the handler. If Not SetConsoleCtrlHandler(New ConsoleEventDelegate(AddressOf Application_ConsoleEvent), True) Then Console.WriteLine("Unable to install console event handler.") Exit Sub End If