C ++异常“跳过” MSVC x64中的try-catch子句 [英] C++ Exception "Skips" Try-Catch Clause in MSVC x64

查看:114
本文介绍了C ++异常“跳过” MSVC x64中的try-catch子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++编写程序。该程序在Win32(x86)上运行良好,最近我尝试在x64上本地编译。当然,这些东西并不能立即起作用。

I'm writing a program in C++. The program has been working fine for Win32 (x86), and recently I've tried compiling it natively for x64. Of course, stuff didn't work right away.

在调试了问题之后,我设法用以下简单的代码片段重现了它:

After debugging the problem, I've managed to reproduce it with this simple code snippet:

class MyException { };

int main()
{
    try {
        for (;;) {
            try {
                std::cout << "Throwing" << std::endl;

                throw MyException();

                if (1 == 0) {
                    continue;
                }
            } catch (const MyException&) {
                std::cout << "Catch 1" << std::endl;
            }
        }
    } catch (const MyException&) {
        std::cout << "Catch 2" << std::endl;
    }

    std::cout << "Done" << std::endl;

    return 0;
}

(我会解释 if(1 = = 0)子句)

在x86上使用MSVC编译此代码时(我使用过2010),结果是预期的:

When compiling this code using MSVC for x86 (I've used 2010), the result is as expected:

Throwing
Catch 1
Throwing
Catch 1
Throwing
Catch 1
Throwing
Catch 1
...

但是,为x64编译此代码会导致:

However, compiling this code for x64 results in:

Throwing
Catch 2
Done

异常会完全跳过内部catch子句!

The exception completely skips the inner catch clause!

仅当 if(1 == 0)子句存在时才会发生在我的代码中。当我删除它时,异常将按预期方式捕获在捕获1中。

This only happens when the if (1 ==0) clause exists in my code. When I remove it, the exception is caught in "Catch 1" as expected.

我尝试使用其他编译器:

I've tried using other compilers:


  • 此错误也出现在VS 2012中。

  • MinGW和MinGW-w64正常工作。

我的问题:这是MSVC错误,还是我缺少C ++中某些未定义的行为?
如果这确实是MSVC错误,我想听听一些有关原因的见识。

My question: is this an MSVC bug, or is this some undefined behavior in C++ I'm missing? If this indeed is an MSVC bug, I'd love to hear some insight on the cause.

谢谢。

推荐答案

此错误可能与编译器优化有关-有趣的是,链接程序在您的发行版中崩溃(理论上将打开完全优化)。

This bug may have something to do with compiler optimization -- it's interesting that the linker crashes in your release build (when full optimization would theoretically be turned on).

您的调试版本是否已完全禁用优化(/ Od)?

Does your debug build have optimization completely disabled (/Od)?

Visual Studio帮助还包含一条语句(在最佳化最佳实践下),不鼓励使用64位代码的try / catch块。

Visual Studio Help also contains a statement (under "Optimization Best Practices") discouraging try/catch blocks in 64-bit code.

如果您在发行版本中关闭了优化功能,则链接器不会崩溃。如果仅删除 continue语句,它也不会崩溃(但会重现不良行为)。

If you turn off optimization in the release build, the linker doesn't crash. It also won't crash (but will reproduce the bad behavior) if you remove just the "continue" statement.

if (1==0) {
//continue;
}

这篇关于C ++异常“跳过” MSVC x64中的try-catch子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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