防止“microsoft Visual c”运行时库调试断言失败”程序崩溃时不会出现窗口
我想知道是否有一种方法可以在引发异常时自动完全终止程序,而不必在出现的 Visual C++ 库窗口上的“中止”、“重试”和“忽略”按钮之间进行选择?
(示例图片)
有什么解决方案吗? (除了显而易见的——修复我的代码!)
I am wondering if there is a way to automatically terminate the program entirely when an exception is thrown, rather than having to choose between the "abort", "retry", and "ignore" buttons on the Visual C++ library window that appears?
(example image)
Is there any solution for this? (aside from the obvious -- fixing my code!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以通过将
_OUT_TO_STDERR
传递到_set_error_mode()
。如果您仍然收到一个消息框,告诉您调用了
abort()
并且您也想禁用它,则还需要调用_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG)
。完整示例:
Yes, you can suppress the message box by passing
_OUT_TO_STDERR
to_set_error_mode()
.In case you still get a message box telling you that
abort()
was called and you want to disable it, too, you additionally need to call_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG)
.Full example:
SetBreakOnAssert(false)
以及您可以(并且必须)执行的许多其他操作来防止这种情况发生。问题是有许多不同类型的错误机制和错误窗口。您发布的是一个 ASSERT,您可以使用 SetBreakOnAssert 禁用它。但您仍然需要处理错误情况,即终止应用程序。还有其他情况会弹出默认的 Windows 对话框。
std::set_terminate
允许您设置一个函数,当程序的任何部分想要终止应用程序时(例如由于双重异常),该函数将被调用。_set_se_translator
允许您指定可将 Win32 异常(例如“访问冲突”)转换为 C++ 异常的函数。_set_invalid_parameter_handler
允许您指定一个函数,每当您的程序调用带有无效参数的 std 函数时,就会调用该函数。 (我认为越界检查属于这一类)。SetBreakOnAssert(false)
and a whole lot of other things you can (and must) do to prevent this. The problem is that there are many different kind of error mechanisms and error windows.The one you posted is an ASSERT, which you could disable with SetBreakOnAssert. But you'll still have to deal with the error situation, i.e. terminate the app. There are other cases where the default Windows dialog pops up.
std::set_terminate
lets you set a function that get's called when any part of your program wants to terminate the application (for example due to a double exception)._set_se_translator
lets you specify a function that can translate a Win32 exception such as "Access Violation" to a C++ exception._set_invalid_parameter_handler
lets you specify a function that is called whenever your program calls a std function with invalid parameters. (I think out-of-bounds checking falls into that category).