无法捕获std :: invalid_argument [英] Unable to catch std::invalid_argument

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

问题描述

我遇到了一个无法跟踪的std :: invalid_argument异常的问题。我使用gcc 4.4.0(windows),pthreads-win32 2.8.0 GC2 dll。

I've run into an issue catching a std::invalid_argument exception that I'm not able to trace. I'm using gcc 4.4.0 (windows), pthreads-win32 2.8.0 the GC2 dll.

基本上,从两个线程(主线程和线程使用pthread_create开始),我尝试在大致相同的时间创建一个类A的实例。构造函数抛出一个std :: invalid_argument,但是它被try / catch块所包围,应该会捕获异常。然而,这并不会发生(很少,只有一个线程可能会捕获异常 - 没有规则,尽管如此)

Basically, from two threads (main thread and a thread started using pthread_create), I try to create an instance of class A at roughly the same time. The constructor throws an std::invalid_argument, but it is surrounded by try/catch blocks which should catch the exception. However, that does not happen (very rarely, only one of the threads might catch the exception - no rule as to which will do it though)

如果我尝试创建只有一个线程上的对象,创建应该是正常的,异常被捕获。如果我在不同的时间创建了两个对象,那么创建应该是正常的,并且异常被捕获。如果我尝试在同一时间创建它们,:: terminate()被调用。

If I attempt to create the object on only one of the threads, the create works as it should, and the exception is caught. If I create the two objects at different times, the create works as it should and the exception is caught. If I try to create them at the same time, ::terminate() gets called.

也许有人有一个想法,为什么会发生这种情况(我排除了标题) :

Maybe someone has an idea of why this happens (I've excluded headers):

void *run(void *ptr)
{
    Sleep(5000);
    try
    {
        A *a = new A(5);
        a->a = 12;
    }
    catch (std::exception &ex)
    {
        printf("t - %s\n", ex.what());
    }
    return NULL;
}

int main(void) {
    pthread_t t;
    if (pthread_create(&t, NULL, run, NULL) != 0)
    {
        printf("No thread\n");
    }
    else
    {
        Sleep(5000);
        try
        {
            A *a = new A(5);
            a->a = 13;
        } catch (std::exception &ex)
        {
            printf("M - %s\n", ex.what());
        }
        pthread_join(t, NULL);
    }
    return 0;
}

class A
{
public:
    A(int a);
    virtual ~A();
    int a;
};
A::A(int a)
{
    throw std::invalid_argument("Invalid!");
}
A::~A(){}

makefile是:

The makefile is:

CXXFLAGS = -O0 -g -Wall -Werror -fmessage-length=0
OBJS =  WOpenTest.o A.o
INCL = -I../pthreads-win32/include 
LIBS =   -lws2_32 -lgdi32 -lpthreadGC2 
LIB_DIRS =  -L ../pthreads-win32/lib 
TARGET = WOpenTest.exe
$(TARGET): $(OBJS)
 $(CXX) -o $(TARGET) $(OBJS) $(LIBS) $(LIB_DIRS) $(INCL)
WOpenTest.o : WOpenTest.cpp
 g++ $(CXXFLAGS) -c WOpenTest.cpp $(INCL)  
A.o : A.cpp A.h
 g++ $(CXXFLAGS) -c A.cpp $(INCL)
all: $(TARGET)
clean:
 rm -f $(OBJS) $(TARGET)

我看到的输出是:


(最常见)$ ./ WOpenTest.exe

(Most frequently) $ ./WOpenTest.exe

此应用程序已请求
运行时以不寻常的
方式终止它。请联系申请人的
支持团队了解更多信息。

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

此应用程序已请求
运行时以不寻常的
方式终止。请联系申请人的
支持小组了解更多信息。
终止调用后抛出一个
实例'std :: invalid_argument'
终止调用递归

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. terminate called after throwing an instance of 'std::invalid_argument' terminate called recursively


$ ./WOpenTest.exe

$ ./WOpenTest.exe

此应用程序已请求
运行时以不寻常的
方式终止它。请联系申请人的
支持小组了解更多信息。
M - 无效!

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


$ ./WOpenTest.exe

$ ./WOpenTest.exe

此应用程序已请求
运行时以不寻常的
方式终止它。请联系申请人的
支持小组了解更多信息。
t - 无效!

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


此应用程序请求
运行时以不寻常的
方式终止它。请联系申请人的
支持团队了解更多信息。

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

此应用程序已请求
运行时以不寻常的
方式终止。请联系申请人的
支持小组了解更多信息。
终止调用后抛出一个
实例的'std :: invalid_argument'

what():无效!

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. terminate called after throwing an instance of 'std::invalid_argument'
what(): Invalid!

有什么想法我应该做什么我不是吗?或者我用pthreads丢失的东西?

Any ideas on what I should be doing and I'm not? Or something I'm missing with pthreads?

推荐答案

在这里发布最终答案,以防有人在将来找到它。问题是gcc 4.4中的一个关键错误:

Posting final answer here in case someone looks for it in the future. The issue is a critical bug in gcc 4.4:

http://n2.nabble.com/gcc-4-4-multi-threaded-exception-handling-thread-specifier -not-working-td3440749.html

这篇关于无法捕获std :: invalid_argument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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