Failed to catch std :: invalid_argument

I ran into a problem catching the std :: invalid_argument exception, which I cannot track. I am using gcc 4.4.0 (windows), pthreads-win32 2.8.0 GC2 DLL.

Basically, from two threads (the main thread and the one starting with pthread_create), I am trying to create an instance of class A at about the same time. The constructor throws std :: invalid_argument, but it is surrounded by try / catch blocks that should catch the exception. However, this does not happen (very rarely, only one of the threads can catch an exception - there is no rule as to what will do this, though)

If I try to create an object for only one of the threads, the creation will work as it should, and an exception will be detected. If I create two objects at different times, the creation works as it should, and the exception is caught. If I try to create them at the same time, :: terminate () is called.

Maybe someone has an idea why this is happening (I excluded the 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:

 CXXFLAGS = -O0 -g -Wall -Werror -fmessage-length=0 OBJS = WOpenTest.o Ao 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) Ao : A.cpp Ah g++ $(CXXFLAGS) -c A.cpp $(INCL) all: $(TARGET) clean: rm -f $(OBJS) $(TARGET) 

The output that I see is:

(Most often) $. / WOpenTest.exe

This application requested Runtime to finish it in an unusual way. Please contact for more information.

This application requested Runtime to finish it in an unusual way. Please contact for more information. call termination after throwing an instance of 'std :: invalid_argument' termination is called recursively

or

$. / WOpenTest.exe

This application requested Runtime to finish it in an unusual way. Please contact for more information. M - Wrong!

or

$. / WOpenTest.exe

This application requested Runtime to finish it in an unusual way. Please contact for more information. t - Wrong!

or

This application requested Runtime to finish it in an unusual way. Please contact for more information.

This application requested Runtime to finish it in an unusual way. Please contact for more information. end of call after throwing instance 'std :: invalid_argument'
what (): Wrong!

Any ideas on what I should do, but I don't? Or is something missing from pthreads?

+1
c ++ exception error-handling
source share
4 answers

Posting the final answer here if someone is looking for it in the future. The problem is a critical error in gcc 4.4:

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

+2
source share

You have come to the conclusion that this is due to the single-threaded libraries that MinGW belongs to. I found a page about configuring NetBeans to use MinGW , and it has the following tip in the section "Multithreading with Posix Streams":

  • Important if you use exception handling: compile the application using the optional "-mthreads" compiler. Otherwise, exception handling will not work reliably, and non-specific failures may occur. Other problems may occur when distributing DLL exceptions to the application. I found some hints of this on the net, but I have no experience, because my DLL does not throw an exception.

It is also described in the GCC documentation for x86 options:

-mthreads

Support for exception throw threads on ' Mingw32 . -mthreads code should compile and link all code with the -mthreads option. When compiling, -mthreads defines -D_MT ; when linking, it is linked in a special library of auxiliary threads -lmingwthrd , which cleans up the data for processing thread exceptions.

Try before writing your own thread-related exceptions.

+2
source share

Try setting the pthreadGCE2 link instead of pthreadGC2 .

+1
source share

I do not see anything wrong.

Try adding catch (...) to see if there is anything else strange. If the exception leaves the thread (i.e. run ()), then pthreads terminates the application. But I do not see what is happening.

As a side note:
try to catch exceptions by const link.

+1
source share

All Articles