
Or in C++11:
boost::scope_guard lolwhat( [&](){ CloseHandle(handle); } );
... lolwhat.dismiss(); What about boost::exception_guard somename( [&](){ CloseHandle(handle); } );
14.09.2012 22:48, Sohail Somani пишет: that calls the function/lambda only if std::uncaught_exception() returns true? Another example: extern "C" int SomeFunc( char **Ret1, char **Ret2 ) { try { *Ret1 = new char[100]; boost::exception_guard Ret1Guard( [&](){ delete [] *Ret1; } ); *Ret2 = new char[100]; boost::exception_guard Ret2Guard( [&](){ delete [] *Ret2; } ); //try to fill the buffers with the data return 0; } catch( std::exception &ex ) { Log(ex); return -1; } } Note that dismisses are not required, and you even can use some macro magic to get rid of the meaningful guard variable names. -- Best regards, Sergey Cheban