home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 33,185 of 33,346   
   =?UTF-8?Q?Tobias=20M=C3=BCller?= to Richard Phillips   
   Re: RAII advice (for a RAII newbie)   
   31 Aug 13 18:42:09   
   
   -september.org> 16d774ed   
   From: troplin@bluewin.ch   
      
   Richard Phillips  wrote:   
   > { Reformatted; please limit your lines to 70 chars -mod }   
   >   
   > Hello all,   
   >   
   > Going forward, I'm keen to start using RAII on projects, but in order   
   > to justify my future evangelism, I want to clarify a few things...   
   >   
   > So, borrowing an example from Jon Kalb's excellent tutorial videos:   
   >   
   > // BAD   
   > {   
   > 	dosomething();   
   > 	cleanup();   
   > }   
   >   
   >   
   > // GOOD (RAII)   
   > {   
   > 	CleanupType cleanup;   
   > 	dosomething();   
   > }   
   >   
   >   
   > // ????   
   > {   
   > 	try   
   > 	{   
   > 		dosomething();   
   > 		cleanup();   
   > 	}   
   > 	catch (const std::exception& ex)   
   > 	{   
   > 		cleanup();   
   > 	}   
   > 	catch (...)   
   > 	{   
   > 		cleanup();   
   > 	}   
   > }   
   >   
   > Why is the 2nd (RAII) approach superior to the third approach?  I   
   > presume there's more to it than "appearance"?  While the third   
   > approach looks more complicated, it doesn't have to worry about a   
   > "CleanupType"...   
      
   This is probably a bit of a misunderstanding of RAII. I would not recommend   
   creating a special cleanup class for every function. Rather design your   
   classes such that every object cleans up its resources itself in its own   
   destructor. The most important thing is to have proper encapsulation.   
      
   Scope guards like your CleanupType are still useful sometimes. The most   
   prominent use case is probably acquiring/releasing of locks.   
      
   In other languages, this can be done ad-hoc with "try-finally", but   
   "finally" does not exist in C++.   
      
   The problems with try-catch (instead of try-finally) are:   
   - you have to catch (and possibly rethrow) all exceptions, even those you   
   don't want to catch at all.   
   - you have to insert cleanup calls for every single "return" or "catch".   
   Don't repeat yourself!   
   - it's difficult to maintain because it's easy to forget to add that   
   cleanup call.   
      
   Tobi   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca