在Windows上捕获访问冲突 [英] Catching access violations on Windows

查看:103
本文介绍了在Windows上捕获访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获应用程序中所有未处理的异常,以便在发生异常时可以保存日志文件。这是使用C ++编写的使用Visual Studio 2013编译的64位Windows应用程序。为了进行测试,我使用了VS生成的默认C ++ Win32项目。

I am trying to catch all unhandled exceptions in my application so I can save a log file when they occurr. This is a 64-bit Windows application compiled using Visual Studio 2013, written in C++. For testing I am using the default C++ Win32 project generated by VS.

我通过使用SetUnhandledExceptionFilter注册处理程序来捕获所有异常。这适用于/ most /情况,但不是全部。捕获所有throw()-n异常,以及大多数硬件异常,例如浮点或访问冲突。不会触发处理程序的代码是:

I am catching all exceptions by registering a handler using SetUnhandledExceptionFilter. This works fine for /most/ cases, but not all. All throw()-n exceptions are caught, and most hardware exceptions like floating point or access violations as well. The code that doesn't trigger the handler is:

std::vector<int> foo(5, 0);
for (auto& f : foo)
    foo.erase(foo.begin() + 1);

相反,我只是得到标准的Windows崩溃对话框,而没有调用异常处理程序。但是,如果我在带有调试器的Visual Studio中运行它,它将正确报告访问冲突异常。其他类型的访问冲突也会触发处理程序:

Instead I just get the standard windows crash dialog box, without my exception handler getting called. If I run it in Visual Studio with debugger attached however, it correctly reports an access violation exception. Other types of access violations trigger the handler as well:

float* ptr = nullptr;
float value = *ptr;

上面的代码触发异常处理程序。

Code above triggers the exception handler.

我也尝试过使用try / catch或捕获SIGSEGV信号,但第一个示例均未触发。中止/终止信号也不会被调用。简而言之,当崩溃发生时,我不会收到任何通知。

I have tried using try/catch or catching SIGSEGV signal too, but neither get triggered with the first example. abort/terminate signals don't get called either. In short I am in no way notified when that crash happens.

我想知道有什么方法可以在崩溃前在应用程序中获得某种通知由于第一个示例引起的访问冲突?由于VS似乎能够检测到它,所以我假设有办法。

I want to know is there any way I can get some kind of a notification in my application before it crashes due to the access violation caused by the first example? Since VS seems to be able to detect it I'm assuming there's a way.

编辑:
我只是想弄清楚我正在运行释放模式下的代码,第一个示例中的错误不是由在调试模式下进行的迭代器越界检查引起的。

I just want to make it clear I'm running the code in release mode and the error in the first example isn't caused by iterator out of bounds check done in debug mode.

EDIT2:
最简单的示例可以使用Win32控制台应用程序提出。在此处查看:
http://pastebin.com/8L1SN5PQ

请确保在发布模式下运行它,而不附加调试器。

Make sure to run it in release mode with no debugger attached.

推荐答案

这类运行时错误的处理方式不同,它们不会产生SEH异常。大致分为编程错误和恶意软件攻击之间。如果您没有连接调试器,那么与未捕获的C ++异常一样有用,默认处理程序将使用__fastfail()调用即时终止。

These kind of runtime errors are handled differently, they don't produce an SEH exception. Roughly classified somewhere between "programming bug" and "malware attack". And about as informative as a uncaught C++ exception if you don't have a debugger attached, the default handler invokes instant death with __fastfail().

在main()函数中调用 _set_invalid_parameter_handler()来更改它们的方式被处理。您可以在自定义处理程序中引发C ++异常,也可以调用RaiseException()来触发您所有的处理程序,或者只是在此处报告它们。赞成后者,您要确保该过程始终被终止。

You'll have to call _set_invalid_parameter_handler() in your main() function to alter the way they are handled. You could throw a C++ exception in your custom handler or call RaiseException() to trigger your catch-em-all handler or just report them right there. Favor the latter, you want to ensure that the process is always terminated.

请注意,您的摘录不是最好的示例。当您在未启用迭代器调试的情况下生成程序时(例如使用默认设置的Release版本),这是UB。 UB不保证您会获得SEH例外。如果这样做,则必须非常小心地编写异常过滤器,它将在仍然使用堆锁的情况下进行调用,因此基本操作无法正常进行。最好的方法是使用命名事件唤醒保护进程。然后执行一个小型转储并终止程序。

Do beware that your snippet is not the best possible example. This is UB when you build your program without iterator debugging enabled, like the Release build with default settings. UB does not guarantee you'll get an SEH exception. And if it does then you'll have to write your exception filter very carefully, it will be called with the heap lock still taken so basic stuff cannot work. Best way is to wakeup a guard process with a named event. Which then takes a minidump and terminates the program.

这篇关于在Windows上捕获访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆