为什么SetUnhandledExceptionFilter不能捕获一些异常,但是AddVectoredExceptionHandler可以做到 [英] Why SetUnhandledExceptionFilter cannot capture some exception but AddVectoredExceptionHandler can do

查看:3549
本文介绍了为什么SetUnhandledExceptionFilter不能捕获一些异常,但是AddVectoredExceptionHandler可以做到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我的函数我传递给 SetUnhandledExceptionFilter 没有得到调用时,异常代码c0000374提高。但它工作正常与异常代码c0000005。
然后我尝试使用 AddVectoredExceptionHandler 代替,它没有问题,处理程序函数get被正确调用。

I have experienced a problem that the function I passed to the SetUnhandledExceptionFilter didn't get called when the exception code c0000374 raising. But it works fine with the exception code c0000005. Then I tried to use the AddVectoredExceptionHandler instead, and it didn't have the problem, the handler function get called correctly.

这是API错误吗?我可以在任何地方使用AddVectoredExceptionHandler,而不是SetUnhandledExceptionFilter?

Is it the API bug? Can I use AddVectoredExceptionHandler instead of SetUnhandledExceptionFilter everywhere?

这两个函数都可以正确使用

The both functions work correctly with

// Exception code c0000005
int* p1 = NULL;
*p1 = 99;

只有AddVectoredExceptionHandler可以捕获此异常。 (为了证明它不依赖于运行时库,我手动引发异常并且结果相同。)

Only AddVectoredExceptionHandler can capture this exception. (To prove it doesn't depend on the runtime library, I raise the exception manually and it results the same.)

// Exception code c0000374
RaiseException(0xc0000374, 0, 0, NULL);

测试程序。

#include <tchar.h>
#include <fstream>
#include <Windows.h>

LONG WINAPI VectoredExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{
    std::ofstream f;
    f.open("VectoredExceptionHandler.txt", std::ios::out | std::ios::trunc);
    f << std::hex << pExceptionInfo->ExceptionRecord->ExceptionCode << std::endl;
    f.close();

    return EXCEPTION_CONTINUE_SEARCH;
}

LONG WINAPI TopLevelExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{
    std::ofstream f;
    f.open("TopLevelExceptionHandler.txt", std::ios::out | std::ios::trunc);
    f << std::hex << pExceptionInfo->ExceptionRecord->ExceptionCode << std::endl;
    f.close();

    return EXCEPTION_CONTINUE_SEARCH;
}


int _tmain(int argc, _TCHAR* argv[])
{
    AddVectoredExceptionHandler(1, VectoredExceptionHandler);
    SetUnhandledExceptionFilter(TopLevelExceptionHandler);

    // Exception code c0000374
    RaiseException(0xc0000374, 0, 0, NULL);     

    // Exception code c0000005
    // int* p1 = NULL;
    // *p1 = 99;        


    return 0;
}


推荐答案

在MSVC CRT启动时:

It happens because of this code in MSVC CRT startup:

    /*
     * Enable app termination when heap corruption is detected on
     * Windows Vista and above. This is a no-op on down-level OS's
     * and enabled by default for 64-bit processes.
     */

    if (!_NoHeapEnableTerminationOnCorruption)
    {
        HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
    }

如果您想停用它( ),将 nohetoc.obj 链接到您的程序。

If you want to disable it (not recommended), link nohetoc.obj to your program.

这篇关于为什么SetUnhandledExceptionFilter不能捕获一些异常,但是AddVectoredExceptionHandler可以做到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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