防止“microsoft Visual c”运行时库调试断言失败”程序崩溃时不会出现窗口

发布于 2025-01-15 10:04:23 字数 214 浏览 1 评论 0原文

我想知道是否有一种方法可以在引发异常时自动完全终止程序,而不必在出现的 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.

(example image)

Is there any solution for this? (aside from the obvious -- fixing my code!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉睡月亮 2025-01-22 10:04:23

是的,您可以通过将 _OUT_TO_STDERR 传递到 _set_error_mode()
如果您仍然收到一个消息框,告诉您调用了 abort() 并且您也想禁用它,则还需要调用 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG)
完整示例:

#include <cassert>
#include <cstdlib>
#include <crtdbg.h>

int main() 
{
  _set_error_mode(_OUT_TO_STDERR);
  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
  assert(false);
}

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:

#include <cassert>
#include <cstdlib>
#include <crtdbg.h>

int main() 
{
  _set_error_mode(_OUT_TO_STDERR);
  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
  assert(false);
}
┼── 2025-01-22 10:04:23

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文