Are you getting abnormal termination of the program?
I believe that your CCriticalSection object will be released by the CSingleLock destructor. The destructor will always be called, since it is an object on the stack. When the user code is thrown, all the stacks between throw and catch in your function will be unwound.
However, there is a chance that some other object in your user code or even the CSingleLock destructor CSingleLock another exception. In this case, the m_CriticalSection object m_CriticalSection not be released properly and std::terminate is called and your program dies.
Here is an example to demonstrate. Note. I use the std::terminate handler function to notify me of the status. You can also use std::uncaught_exception to see if there are any uncaught exceptions. This one has some nice discussion and sample code.
struct S { S() { std::cout << __FUNCTION__ << std::endl; } ~S() { throw __FUNCTION__; std::cout << __FUNCTION__ << std::endl; } }; void func() { try { S s; { throw 42; } } catch(int e) { std::cout << "Exception: " << e << std::endl; } } void rip() { std::cout << " help me, O mighty Lord!\n";
Read this FAQ for clarity.
Can I solve this problem by putting CSingleLock outside the try block?
It's hard to say without looking at the stack and errors / crashes. Why don't you give it a try. It can also introduce a subtle bug, hiding the real problem.
source share