Solved

A problem about SEH and C++ Exception handle

Posted on 2001-07-22
3
807 Views
Last Modified: 2012-05-04
I write a function in a class.But when i compile,the VC++ compiler show me
"error C2712: Cannot use __try in functions that require object unwinding".
It seems that the "throw" can't be used with __try,is that true and why?
Could you tell me
what is "object unwinding" and what are the differences between "object unwinding" and "global unwinding"?Thanks

BOOL CThreadPool::Run(LPTHREADPROC proc,void *pParam){
     if (proc==NULL)
          throw std::invalid_argument(TEXT("Thread function can't be null"));
     
     __try{
          WaitForSingleObject(m_hMutex,INFINITE);
                .
                .
                .
          return FALSE;
     }
     __finally{
          ReleaseMutex(m_hMutex);
     }
     return TRUE;
}
0
Comment
Question by:jatom
3 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6306021
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6306042
>> It seems that the "throw" can't be used with __try,is that true and why?
VC supports 2 bascially unrelated exception mechanisms.  

First there is SEH (structured exception handling).  This mechanism is built into windows and is not langauge depdant.  i.e. it can be used in C or Pascal ot other langauges in windows programs.   since this is language independant, you can't expect it to handle language specifc issues--like calling destructors.  The Windows OS doesn't know how to handle calling destructors during a stack unwind.  That would depend upon not only the program's lanuage, but also the exact compiler used to create the program.  So in SEH destrctors are not called during stack unwind and there may be other problems as well with other langauge-specific features.

The second exception mechanism is ordianry C++ exceptions.  There are part of the C++ language. and so they do not relly on the OS in any way.  Since they are part of the lanague, they can do language-specific operations, like calling destructors.

If you are writting in C++, its probably best to use C++ exceptions (try...catch) not SEH exceptions (_try...__except) for all your exceptions.   (Al the ones you generate) since they are much more compatible with C++ and are much more portable to other OSs.   If you must use SEH (since the OS uses SEH when it detects a problem, The OS can't throuw a C++ exception since that is langauge-specific), then you should probably convert the structured exception into a C++ exception.   You can do this by either carchign the SE right away and then throwing a C++ exception.  Or by using _set_se_translator().  This sets a function that is called each time a SE occurs.  You can set this to be a function that then throws a C++ exception.  i.e. you write a functionm that converts the SE to a C++ exception.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6306470
The simplest solution is separating both mechanisms in two different functions:

BOOL CThreadPool::Run(LPTHREADPROC proc,void *pParam){
    if (proc==NULL)
         throw std::invalid_argument(TEXT("Thread function can't be null"));

    MutexWait ( m_hMutex, INFINITE);    

    return TRUE;
}

void MutexWait ( HANDLE hMutex, DWORD dwTimeout)
{
    __try{
         WaitForSingleObject(hMutex,dwTimeout);
               .
               .
               .
         return FALSE;
    }
    __finally{
         ReleaseMutex(hMutex);
    }
}

so you'll get rid of the problem.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now