Saturday, June 12, 2010

RAII vs finally

Since I'm pretty new to C++, I wasn't too deeply familiar with RAII; like most Schemers I just thought of it as "C++'s version of dynamic-wind."

This week I learned an important distinction between C++ destructors and Java's finally. The latter, of course, unilaterally executes when the body terminates, regardless of how or when it terminates. The thing that gives destructors more expressiveness for dealing with cleanup is that they only execute for the objects that have been initialized. This means that if control exits a block after only half of the stack-local objects have been constructed, only those half of the objects have their destructors invoked. With finally, all that bookkeeping is the responsibility of the programmer.

(That said, I still see RAII used all over the place to construct awkward, special-purpose classes whose sole purpose is to run some cleanup code. In these cases, having to create a named object and a named class to go along with it is pretty perverse.)

28 comments:

Jeremy Fincher said...

The other major benefit of RAII over a "finally" block is that the former cannot be forgotten: programmers can easily write poor code by forgetting a finally block, but they cannot make the same mistake if the functions they call return only resources managed by classes with appropriate destructors.

steck said...

Lately, I've thought of Dorothy's departure from Oz as a non-local exit, like an exception throw or continuation invocation, combined with resource/object cleanup.

This particular metaphor is aided by the fact that Kansas begins with a `k'. And you can think of the words "I'm melting!" as the sound of destructors in action.

tonyg said...

I like Scheme's call-with-input-file (and friends) and Python's "with" statement as approaches to this problem. Both are used to bound regions in which a resource is available for use, with automatic cleanup as appropriate for the type of control transfer used to exit the block. I guess they share some of the problems of RAII, actually, with respect to the need for out-of-line pieces of code for resource management. Does "nested finally" help avoid the manual bookkeeping you mention? Have you been considering any alternatives that combine the virtues of both RAII and finally?

Jeff Foster said...

(That said, I still see RAII used all over the place to construct awkward, special-purpose classes whose sole purpose is to run some cleanup code. In these cases, having to create a named object and a named class to go along with it is pretty perverse.)

You can get rid of some of this boilerplate with smart pointer classes that take a function to call when the pointer goes out of scope. Something like boost::shared_ptr allows you to pass a function in to be called when it goes out of scope.

Sage said...

steck - that is inspired! Hope you don't mind but i'm having that little visual image to help my memory along as well!

verte said...

We recently had a very interesting discussion on this subject on cap-talk that you might enjoy: http://www.eros-os.org/pipermail/cap-talk/2011-March/thread.html#14632

3D4PC-Ryan-Newby-3D- said...

Very interesting but over my head, Hardware Man.

Rithi Rawat said...

Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
Check out : machine learning training in chennai
machine learning projects in chennai
machine learning tution in chennai
artificial intelligence and machine learning course in chennai

Anonymous said...

Thank you for sharing
https://aab-edu.net/

cynthiawilliams said...

Outstanding blog with lots of information. Keep posting more like this.
Machine Learning course in Chennai
Machine Learning course
Machine Learning institute in Chennai
Data Science Training in Chennai
Data Science Training Institute in Chennai
R Programming Training in Chennai
Data Analytics Training in Chennai
Data Science Training in Anna Nagar

DedicatedHosting4u said...

Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks .

DedicatedHosting4u.com


MindtechAffiliates said...

Consistently learning the new skills and using them to improve myself in the form of performance for betterment.

Thanks
Cpa offers

raj khan said...




Soma pill is very effective as a painkiller that helps us to get effective relief from pain. This cannot cure pain. Yet when it is taken with proper rest, it can offer you effective relief from pain.
This painkiller can offer you relief from any kind of pain. But Soma 350 mg is best in treating acute pain. Acute pain is a type of short-term pain which is sharp in nature. Buy Soma 350 mg online to get relief from your acute pain.

https://globalonlinepills.com/product/soma-350-mg/


Buy Soma 350 mg
Soma Pill
Buy Soma 350 mg online



Buy Soma 350 mg online
Soma Pill
Buy Soma 350 mg

raj khan said...




Soma pill is very effective as a painkiller that helps us to get effective relief from pain. This cannot cure pain. Yet when it is taken with proper rest, it can offer you effective relief from pain.
This painkiller can offer you relief from any kind of pain. But Soma 350 mg is best in treating acute pain. Acute pain is a type of short-term pain which is sharp in nature. Buy Soma 350 mg online to get relief from your acute pain.

https://globalonlinepills.com/product/soma-350-mg/


Buy Soma 350 mg
Soma Pill
Buy Soma 350 mg online



Buy Soma 350 mg online
Soma Pill
Buy Soma 350 mg

Pankaj Singh said...

Great and valuable information put in this blog, thank you so much for sharing this information with us. Visit OGEN Infosystem for responsive web design and SEO Service.
SEO Service in Delhi

Anonymous said...

QuickBooks File Doctor is the program that helps the user to troubleshoot any type of QuickBooks error. These errors may have been hindering the processes.

Amara James said...

Are you all aware about the information on many movies downloading websites like moviesbaba bollywood?
If not then also no need to worry as this article will surely going to help you to get all information about the given topic and about 123movierulz.
Along with it jalsamoviez.pw also shows the best and latest movies online from where you can even download them.
Thanks.

Alex williams said...

We support all types of HP printer troubleshooting and service. Just enter the model number of your printer in 123.hp.com/setup to identify the software and drivers your printer requires. Download and install it in your mac and 'Run' the file. The process is easy however if you have any doubts or queries regarding HP printers contact us.

aragates said...

I take pleasure in introducing myself as an expert who could help you in the selection and activation of the best channels available. There are so many channels available today on television. There is every chance of you missing the best channel because of not being aware of it.

visit my site : espn.com/activate

dhanuvarma said...

https://shareitapk.xyz/
SHAREit apk

Roku Hdcp said...

The special features of PBS Channel include the PBS Passport that it provides. PBS Passport benefits from membership in PBS stations that provide access to a professional on-demand on-line public television archive to qualifying subscribers.if you need more information about pbs channel visit pbs.org/activate

Roku Hdcp said...
This comment has been removed by the author.
alexcosta said...

thanks for information.Tubi is a video streaming service, not unlike Netflix, Hulu, Amazon Prime Video and others, with an extensive library of TV shows and movies that you can watch free of charge from your phone, smart TV or streaming device. If you need any assistance tubi.tv/activate on roku please contact our technical support +1-844-525-1240 to get more information.

Ryan phillip said...

All our words are but crumbs that fall down from the feast of the mind. You should know the secret to express yourself through the words to be call as a Writer. I think I know that secret very well. Read my words in these blogs given below.

My Words : pbs.org/activate | pbskids.org/activate

elenasmith said...

Hello, Thanks for sharing the amazing blog. It's very interesting.
here are the office setup and installation at https://officesetup.support/.You need to have an Active Office Subscription to download Office on your PC to enjoy all the features. The office is one of the best software that will give you support for all your pen devices as well as flexibility. So, get your office setup www.office.com/setup

pollard Holmes said...

I am a Young fellow who loves to write. I'll quote myself by saying that I am a human by nature, writer by profession and a mighty traveler by passion. I'll always want to write about the things that people are using in their day to day life.

My Creations : abc.com/activate

Micwhite Watch Roku said...

Choose Pandora's music that you love. Create custom stations from your favorite songs and artists or upgrade to Pandora Premium's songs, albums, and playlists on-demand. Get the best from the best and all you need to do is activate Pandora on Roku. Navigate to the channel store from the home screen and search for Pandora. Find a code on the screen after which you will have to navigate to the Pandora.com/activate roku enter code. Enter the code and click on submit to successfully add the channel. Now, you are free to listen to any of your favorite songs on Pandora. For any additional queries, contact the active customer care team @ +1-888-298-2680, working 24X7

Rahul Aniket said...

tamilgun