Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

c# - NullReferenceException with no stack trace when hooking SetConsoleCtrlHandler

Using code to hook the console close event from this thread, I sometimes get a NullReferenceException with no stacktrace (most of the times I don't). It happens in both release and debug and "break when an exception is thrown" doesn't help (it breaks, but the stack trace is still empty). I never get this exception when I exit my application normally (which is hitting enter and thus releasing a Console.ReadLine). The Application event log has 2 entries:

Application: MyApp.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException Stack:

And:

Faulting application name: Gateway.exe, version: 1.0.0.0, time stamp: 0x4e284101 Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000 Exception code: 0xc0000005 Fault offset: 0x004d41ce Faulting process id: 0xf00 Faulting application start time: 0x01cc47b827e19a6e Faulting application path: C:devMyApp.exe Faulting module path: unknown Report Id: 689c1caa-b3ab-11e0-ba1b-00247e777f12

Google has revealed some bugs and issues with SetConsoleCtrlHandler, so I'm wondering if this is a lost battle.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The most typical issue with code like that is not keeping a reference to the delegate instance. The one you pass as the first argument to SetConsoleCtrlHandler(). The garbage collector cannot see references held to a delegate object by unmanaged code. So this will, eventually, bomb when the garbage collector runs:

 SetConsoleCtrlHandler(Handler, true);

which is the exact same thing as

 SetConsoleCtrlHandler(new EventHandler(Handler), true);

assuming you used the types in the linked code. The author of that code carefully avoided this issue by making _handler a static variable. As opposed to the temporary delegate instance that is created by the previous two lines of code. Storing it in a static variable ensures it stays referenced for the life of the program. The Right Thing to do in this particular case since you are actually interested in the events until the program ends.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...