在调用返回结构的函数后抛出异常时,堆栈展开过程失败

Stack unwind procedure failing when throwing exception after calling a function that returns a structure.

本文关键字:堆栈 失败 过程 抛出异常 返回 调用 结构 函数      更新时间:2023-10-16

本周早些时候我已经发布了另外两个问题,关于异常以及为什么我的程序不处理异常。我已经从不必要的代码中"脱衣"了我的程序,下面是:

#include <string>
#include <stdexcept>
#include <iostream>
class some_class
{
    public:
    some_class(const some_class &);
    some_class(const char *);
    std::string m_id;
};
some_class::some_class(const char *p_id) :
    m_id(p_id)
{
}
some_class::some_class(const some_class &p_that) :
    m_id(p_that.m_id)
{
}
extern some_class return_a_struct(const char *p_id); 
int run()
{
    some_class l = return_a_struct("john");
    throw std::runtime_error("something bad happened");
    return 0;
}
extern "C" int main(int, char **)
{
    try
    {
        run();
    }
    catch(const std::exception &p)
    {
        std::cout << p.what() << std::endl;
    }
    return 0;
}
some_class return_a_struct(const char *p_id)
{
    return some_class(p_id);
}

输出应该是:somethingbad发生

根据我抛出的异常(std::runtime_error)。

在run()中,我调用返回some_class的函数。然后将返回的对象复制构造为我分配给它的对象。到目前为止,一切顺利。但是当我抛出异常时,程序就永远不会到达main函数中的catch处理程序。它崩溃时显示以下消息:

This application has requested the Runtime to terminate it in an unusual way
Please contact the application's support team for more information.

如果我提交对return_a_struct()的调用,则不会发生这种情况。

问题是:这是gcc (MinGW最新版本在Windows 7上运行的一部分)中的错误,还是我做错了什么?还有别的办法吗?

GCC-options:

gcc -fexceptions -g3 test_case.cpp -l libstdc++ -o test_case.exe

g++ test_case.cpp -o test_case.exe