views:

144

answers:

3

I came across this code in a large codebase

DWORD WINAPI ThreadFunc (LPVOID lpParam)
{ 
    int *x = 0;
    *x = 1234; // Access violation

    return 0; 
}

void Manager::Crash ()
{
    Log("Received a remote command to crash Server.");

    DWORD dwThreadId, dwThrdParam = 1; 
    HANDLE hThread = ::CreateThread(NULL, 0, ThreadFunc, &dwThrdParam, 0, &dwThreadId);
} 

My question is: Why is it using a thread? Would it be more or less threadsafe if the code in ThreadFunc was done directly in Manager::Crash? I am reluctant to make changes in case I remove the crash.

+5  A: 

He doesn't want to handle the exception that occurs. The original thread that received the Manager::Crash continues. An AV exception does not necessarily terminate the process. Although in this case the fact that is not handled by a __try/__except block (note, is a SEH try block, not the C++ one) then the unhandled second chance exception will terminate the process. But perhaps he want to force Dr. Watson/WER to jump in, or the post-mortem debugger to launch, or to break into current debugger. Who knows...

Actually, d'oh! IF the main thread does have a SEH handler installed, it will not crash the process. QED.

Remus Rusanu
I'd word that as "He doesn't want any thrown exceptions handled. The original thread might have a SEH block, and would catch it."
GMan
@GMan: Yeap, I'm pretty sure that's it, avoiding any SEH installed in the main thread(s). Saw your comment up right after I edited my 'd'oh' moment :)
Remus Rusanu
This is totally something that deserves a comment in the code.
jamesdlin
If you downvote, plz explain why
Remus Rusanu
A: 

I don't see a reason for this to be in a separate thread unless you have more work to do after the thread that we are not seeing. I'm guessing that the main thread catches it for some reason. I'd rather catch it when it happens then have it percolate to the top personally.

Why crash the server? Sounds like a poor design choice to me.

0A0D
How about if you need to test your post-mortem dumper and error reporting infrastructure? A way to crash the process on-demand it sure is handy.
Remus Rusanu
@Remus: That's why there's unit testing
0A0D
@0A0D: Seems like this could well be part of some unit testing infrastructure. :)
quixoto
@quixoto: More like integration testing to me
0A0D
@0A0D: its fine even in a production server as long as the authority to **use** the crash server command is suitably restricted. Don't want to give out a DOS attack for free, naturally.
RBerteig
If it was for unit testing, you would want it to get into the SEH.
Or maybe it's a section of "unreachable code", and crashing the server is safer than continuing in an unknown/unexpected mode of operation.
Tom
@0A0D: Our code is too big to unit test!
Ariel Bold
@Ariel: Blasphemy!
0A0D
@0A0D: I don't understand.
Ariel Bold
+1  A: 

You'd need to ask whichever programmer wrote it in the first place, but probably the rationale behind crashing in a thread like that was to get around an exception handler designed to catch segfaults in the main thread. This might be a good time to break out your source control's time-lapse view and email the perpetrator.

That's also an unnecessarily clumsy way to induce a CPU exception, by the way. You can trigger a crash or breakpoint directly by using eg __debugbreak() or an inline assembly int 3. That will cause the program to break into the debugger immediately, which by default in most MSVC programs will dump core if no debugger is attached.

Crashworks
+1 for noting that this is a fairly JV way to induce a crash if you want to be sure that one happens.
quixoto