开发者

Win32 Thread Exits Unexpectedly

I'm writing a C++ application. I realized that one of my worker threads may terminate unexpectedly. The (VS 2005) debug log says:

The thread 'Win32 Thread' (0x5d98) has exited with code -858993460 (0xcccccccc).

I surrounded all the worker thread code with a try/catch block. So, if the reason was an exception, I would catch it. But I can't:

try{
   ...
   Connection* conn = connectionPool->getConnection(); // unexpected exit occurs here
   ...
} catch(exception& e) {
   ...
}

I have ten threads running concurrently, and only one of them gets crashed after some time, while the others continue running (and get开发者_JAVA技巧ting new [OCCI] connections).

Is there an exception type that is not caught by "exception"? Or what do I not know about threads/exceptions?

Thanks.


Not all errors raise C++ exceptions that can be caught with the C++ try...catch mechanism. For example, division by zero will not raise a C++ exception, but will cause undefined behaviour which may well make your application exit.

However, your code may be throwing things that are not derived from std::exception , so you may want to rewrite:

try{
   Connection* conn = connectionPool->getConnection(); // unexpected exit occurs here
} 
catch(exception& e) {
    // handle things derived from std::exception
}
catch ( ... ) {
   // handle things that are not so derived
}

which will deal with things like throw "eeek!";

Also, but nothing to do with the problem, you should normally catch exceptins via a const reference.


Is there an exception type that is not caught by "exception"?

Yes, SEH exceptions. To catch them you need to either use __try/__except MSVC extension (see Structured Exception Handling), or write a global SEH/VEH handler (see SetUnhandledExceptionFilter and AddVectoredExceptionHandler).


The easiest way to find the problem would be to run your application under a debugger and enable breaking on Win32 Exceptions. Whenever a Win32 Exception is encountered, the application would break into the debugger and you can find out whats going wrong.

If you are not debugging and want to catch a win32 structured exception, you have to use _set_se_translator api to set a translator function. The registered function will be called whenever there is a Win32 exception and you get a chance to convert it to a C++ exception of your choice.


catch(exception& e) catches C++ exceptions derived from std::exception, and nothing else.

It doesn't catch C++ exceptions that are not derived from that class (If I do throw 42, it won't be caught for example), and it doesn't catch exceptions or errors at the system level.

Windows uses Structured Exception Handling (SEH) to signal errors, and those are not caught with a plain C++ catch statement. This might include errors like division by zero, as well as access violations or pretty much anything else that might go wrong at the OS or hardware level.

The docs have a nice explanation of how to catch SEH exceptions.


I found an important clue. When you close a handle with CloseHandle function, the thread exits with code 0xCCCCCCCC. With the help of this clue, I realized that in a very rare situation, I close my thread's handle even though the thread's working. Why does it exit exactly while getting connection? That also has an explanation, but it's related to the structure of the code, which may be difficult to explain here.

Thank you all, who brainstormed with me on the exceptions issue :$.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜