C++ 测试主体中抛出代码为 0xc0000005 的 SEH 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13157671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:02:36  来源:igfitidea点击:

SEH exception with code 0xc0000005 thrown in the test body

c++visual-c++tddgoogletest

提问by chintan s

I am writing a test using GoogleTest for the following class and I am getting the above error.

我正在使用 GoogleTest 为以下课程编写测试,但出现上述错误。

class Base
{
    // Other Functions;

    CSig objSig[50];
}

The Class CSig is as follows:

CSig 类如下:

class CSig
{
    //... constructor, destructor(empty) and some functions
    CMod *objMod;
    CDemod *objDemod;
}

CSig :: CSig
{
    bIsInitialised = false;

    for (int i=0; i<MAX_NUM; i++)
    {
        PStrokePrev[i] = 0.0;
    }
}

However, when I discard CSig objSig[50], the tests run fine.

但是,当我丢弃时CSig objSig[50],测试运行良好。

What can I do to solve this issue? Also, I need to have CSig objSig[50]in the Base class.

我能做些什么来解决这个问题?另外,我需要CSig objSig[50]在 Base 类中。

回答by MadScientist

A SEH (Structured Exception Handling) exception is not a C++-exception that can be handled using c++-language constructs (try-catch) but it is raised from windows itself and points to some fundamental flaw. SEH-exceptions are very annoying because they do not cause normal stack unwinding which can lead to unclosed files or not-unlocked mutexes that should normally cleared by the destructors of the owning object. I have encountered SEH-exceptions when accessing memory that does not belong to the current process so I recommend looking at memory-related instructions in the constructor and destructor of CSig. You can read about SEH, for instance, here

SEH(结构化异常处理)异常不是可以使用 C++ 语言构造(try-catch)处理的 C++ 异常,但它是从 Windows 本身引发的,并指向一些基本缺陷。SEH 异常非常烦人,因为它们不会导致正常的堆栈展开,这可能导致未关闭的文件或未解锁的互斥体,这些互斥体通常应该由拥有对象的析构函数清除。我在访问不属于当前进程的内存时遇到了 SEH 异常,因此我建议在 CSig 的构造函数和析构函数中查看与内存相关的指令。例如,您可以在此处阅读有关 SEH 的信息

回答by Michele

The way I just found the problem was that in Visual Studio I went to Debug->Exceptions, and checked everything in the first column. Then run/debug your unit tests, and it will throw an exception on the line that the problem is at. That's where you need to debug/fix it.

我刚刚发现问题的方式是,在 Visual Studio 中,我转到了 Debug->Exceptions,并检查了第一列中的所有内容。然后运行/调试您的单元测试,它将在问题所在的行上抛出异常。这就是您需要调试/修复它的地方。

回答by MutantXenu

I ran into this very problem using GoogleTest with Visual Studio 2010. Our setup involves creating a library for the GoogleTest Frameworks, which is then linked against our individual Unit Tests. I recently updated the Frameworks support and recompiled it from scratch. After doing this, I encountered the exception described above.

我在 Visual Studio 2010 中使用 GoogleTest 时遇到了这个问题。我们的设置涉及为 GoogleTest 框架创建一个库,然后将其链接到我们的各个单元测试。我最近更新了框架支持并从头开始重新编译。这样做后,我遇到了上述异常。

After a bit of digging, I discovered that the 'Struct Member Alignment' setting was the culprit:

经过一番挖掘,我发现“结构成员对齐”设置是罪魁祸首:

Project properties > Configuration Properties > C/C++ > Code Generation > Struct Member Alignment

项目属性 > 配置属性 > C/C++ > 代码生成 > 结构成员对齐

While the Frameworks project had the setting set to 'default', the corresponding Unit Test project had it configured to "1 Byte /Zp1". Once I changed them to have the same alignment, the problem went away.

虽然框架项目的设置设置为“默认”,但相应的单元测试项目将其配置为“1 字节/Zp1”。一旦我将它们更改为具有相同的对齐方式,问题就消失了。

回答by Chiel ten Brinke

For me it appeared to be a null reference error. Some method was called on a nullptr and for reasons unclear to me it didn't fail immediately but just started executing. The SEH error presumably occurred as soon as unallocated memory was accessed. So check for null pointers!

对我来说,这似乎是一个空引用错误。在 nullptr 上调用了一些方法,由于我不清楚的原因,它并没有立即失败,而是开始执行。SEH 错误可能在访问未分配的内存时立即发生。所以检查空指针!

回答by TrustyCoder

I am having a similar issue and its pertaining to un-initialized variables and running the test in release build. I had a char * un-initialized after which initialized to NULL seems to have fixed the issue.

我有一个类似的问题,它与未初始化的变量和在发布版本中运行测试有关。我有一个 char * 未初始化,之后初始化为 NULL 似乎解决了这个问题。

回答by Jax297

Ran into this problem using msvc in qt. SEH was thrown almost randomly, for no apparentreasons. (read: didn't have any time left to look into it) After NOT finding the right solution and the tests seemed to work for non-windows users, I switched to MinGW which ran the tests normally.

在 qt 中使用 msvc 遇到了这个问题。SEH 几乎是随机抛出的,没有明显的原因。(阅读:没有时间研究它)在没有找到正确的解决方案并且测试似乎适用于非 Windows 用户之后,我切换到正常运行测试的 MinGW。

回答by Chaitanya

If you are using Visual Studio 2013, check the Thrown box for Win32 exceptions (specifically access violation) in Debug>Exceptions. This will let you debug which line has the issue. This might be useful since the debugger will not break if your program normally raises other exceptions.

如果您使用的是 Visual Studio 2013,请在“调试”>“异常”中选中 Win32 异常(特别是访问冲突)的抛出框。这将让您调试哪一行有问题。这可能很有用,因为如果您的程序通常引发其他异常,调试器不会中断。