Cleanest way to explicitly crash an application?
My application has custom crash-handling built-in (see John Robbins' excellent book about "Debugging Windows Applications"). To test this functionality, I always used the Windows function DebugBreak() a开发者_开发问答nd this always worked perfectly. But since Windows 7, calling this function just says "A breakpoint has been reached" and stops the application without calling my crash handlers.
I could always put this code in my application to test the crash-functionality:
int *ptr = (int *)0xdeadbeef;
*ptr = 123456789;
Or even add several cases, just in case 0xdeadbeef is a valid address:
int *ptr = (int *)0xdeadbeef;
*ptr = 123456789;
ptr = (int *)0L;
*ptr = 123456789;
ptr = (int *)0xffffffff;
*ptr = 123456789;
But I was wondering: isn't there a cleaner way to crash your application under Windows?
Just create a null pointer to an object with some member functions a try and call one of them? And maybe do it in a function so you know what it daoes
void CrashApp()
{
MyObject * ptr = 0;
ptr->Function();
}
Definitely the easiest way and pretty clear whats going on
You can use __debugbreak() intrinsic instead of DebugBreak() function. This does not say anything and crashes with EXCEPTION_BREAKPOINT
RaiseException() is yet another way to crash.
精彩评论