try finally and object unwinding limitation - what is the alternative?
-
2012년 4월 18일 수요일 오후 1:54
getting a compile error, cannot use __try in functions that require object unwinding. Is there a way to use try finally with std::string?
I don't want to work against the language. try finally is pretty nice, but it is not c++ ? In this case I want to make sure I release a Mutex lock? What is the C++ way to make sure a resource is always released?
thanks,
void WriteToLogFile( ) { HANDLE hLock = NULL ; __try { hLock = ::CreateMutex(NULL, false, L"{A1CCEE5E-9F3F-4D84-A3A5-9CCFFDAE7C3B}") ; if ( hLock != NULL ) { auto waitResult = ::WaitForSingleObject(hLock, INFINITE) ; if ( waitResult == WAIT_OBJECT_0) { std::string msg = "abc" ; } } } __finally { if ( hLock != NULL ) ::CloseHandle(hLock) ; hLock = NULL ; } }
모든 응답
-
2012년 4월 18일 수요일 오후 2:08
Well, the easiest way would be to wrap the handle in an object itself.
If for some reason you don't want to do that, use the C++ exception handling catch all block.
try { } catch(...) //will catch all exceptions) { }This will have a similar effect.
Wrapping the handle into a C++ object is the prefered way though. The class destructor is always guaranteed to be called when an exception is thrown so you don't even need a try/catch block in that function.
This is a signature
Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.- 답변으로 표시됨 Steve Richter 2012년 4월 18일 수요일 오후 2:21
-
2012년 4월 18일 수요일 오후 2:41
On 18/04/2012 15:54, Steve Richter wrote:
getting a compile error, cannot use __try in functions that require object unwinding. Is there a way to use try finally with std::string?
I don't want to work against the language. try finally is pretty nice, but it is not c++ ? In this case I want to make sure I release a Mutex lock? What is the C++ way to make sure a resource is always released?__try/__finally is for structured Win32 exceptions, not C++ exceptions.
As already written, in C++ just use RAII and destructors, and you don't need this __try/__finally pattern.
Simple sample code:
// Simple C++ RAII wrapper to raw HANDLE class Handle { // Ban copy: private: Handle(const Handle&); Handle& operator=(const Handle&); // Wrapped raw HANDLE HANDLE m_h; public: explicit Handle(HANDLE h) : m_h(h) {} ~Handle() { if (m_h != NULL) ::CloseHandle(m_h); } HANDLE Get() const { return m_h; } };With the above C++ wrapper, your code simply becomes:
void WriteToLogFile() { Handle hLock( ::CreateMutex(NULL, FALSE, L"{A1CCEE5E-9F3F-4D84-A3A5-9CCFFDAE7C3B}"); if (hLock.Get() != NULL) { ... } }No try/catch: C++ destructors automatically do proper cleanup.
For a more complete convenient template class for wrapping raw handles, see this:http://msdn.microsoft.com/en-us/magazine/hh288076.aspx
Giovanni
- 답변으로 표시됨 Steve Richter 2012년 4월 18일 수요일 오후 4:13
-
2012년 4월 18일 수요일 오후 5:56
I don't want to work against the language. try finally is pretty nice, but it is not c++ ?
Yes, it looks like c++ and SEH are finally divorsed. In the days of yore they could be used together, and such use even was endorsed in the Jeffrey Richter's C++ book.
--pa

