问题
There\'s a common error that gets thrown by the Visual C Runtime:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application\'s support team for more information.
What does this error message actually mean?
Let me use a parable to explain exactly what i\'m asking.
If I see a message:
Exception: access violation (0xc0000005), Address 0x702be865
This access violation has nothing to do with sexual harassment, or someone trying to break into my computer (any more than General Failure was a brigadier general who was trying to read my C drive, or that you could be hauled off to jail for performing an illegal operation in Windows 95).
In this case, access violation corresponds to the constant EXCEPTION_ACCESS_VIOLATION
(declared in winbase.h
with value 0xC0000005). This constant one possible exception error code that can be returned in an EXCEPTION_RECORD structure. The code ACCESS_VIOLATION
means that the program tried to read or write to an address in memory that it shouldn\'t be. If you try to read from a memory address that was never allocated, then you\'re doing something horribly bad - and the exception tells you so.
It is usually caused when a program has a pointer to memory that is not, or is no longer, valid. The solution is stop trying to access memory that isn\'t valid.
Note: I\'m not asking:
- why is program x getting a C0000005 error?
- why is my code getting an access violation?
- how do I debug an access violation?
So if I asked you, what causes an access violation, you wouldn\'t tell me to check the stack trace, or watch the output window, or to post sample code. You would say, \"It is from trying to access memory that isn\'t valid.\"
Back to my question. What does the following error mean:
This application has requested the Runtime to terminate in an unusual way.
I am (fairly) certain that the Microsoft Visual C Runtime library does not have a function:
void TerminateRuntime(bool UnusualWay);
So I have to try to figure out what it actually means:
- What does it mean to terminate the visual C runtime library? (msvcrt is a dll; you don\'t terminate it, you just don\'t use it anymore)
- What would be a usual way to terminate MSVCRT?
- Would someone choose to terminate it in an unusual way?
- Is today\'s unusual way actually a long since deprecated form of what used to be the usual way?
- If I was (mistakenly) terminating it in an unusual way, what would I do to terminate it in the usual way?
In other words: what error is the MSVCRT catching, and hiding behind the uninformative error message?
回答1:
You get that message when abort() function is called.
From MSDN:
abort
Aborts the current process and returns an error code.
void abort( void );
Return Value
abort does not return control to the calling process. By default, it terminates the current process and returns an exit code of 3.
Remarks
By default, the abort routine prints the message:
"This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."
It seems that in the recent version of the VC runtime, the message has been replaced by "abort() has been called" perhaps to clarify what it really means. If you want to reproduce that message, use an old VC runtime( VC++ 6.0 for sure), and call abort()
.
Internally, when abort() is called, it calls a function _amsg_exit, defined in internal.h, which basically "emits the runtime error message to stderr for console applications, or displays the message in a message box for Windows applications". The error message for "This application has requested the Runtime to terminate it in an unusual way" is defined in the cmsgs.h:
cmsgs.h:
#define _RT_ABORT_TXT "" EOL "This application has requested the Runtime to terminate it in an unusual way.\nPlease contact the application's support team for more information." EOL
and the error code that gets passed in (_RT_ABORT
) is defined in rterr.h:
rterr.h
#define _RT_ABORT 10 /* Abnormal program termination */
So alternatively, you can reproduce this by calling _amsg_exit( _RT_ABORT )
Update by question poster: Two weeks after i asked this question, Raymond Chen answered it in his own blog:
You're running your program, and then it suddenly exits with the message This application has requested the Runtime to terminate it in an unusual way. What happened?
That message is printed by the C runtime function abort, the same function that also causes your program to terminate with exit code 3.
Your program might call abort explicitly, or it might end up being called implicitly by the runtime library itself.
- The assert macro calls abort when an assertion fails.
- By default, the terminate function calls abort.
The C++ standard spells out the conditions under which
terminate
is called, and it's quite a long list, so I won't bother repeating them here. Consult your favorite copy of the C++ standard for details. (The most common reason is throwing an unhandled exception.)
来源:https://stackoverflow.com/questions/8177152/what-is-the-cause-of-this-application-has-requested-the-runtime-to-terminate-it