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.)

76 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?

Unknown 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...
This comment has been removed by the author.
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

James Whatt 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

datasciencecourse said...

data science course bangalore is the best data science course

pollard Holmes said...

Got confused in proceeding the activation setup for your Roku account? This instruction might help you to proceed with the activation with the clear steps. Complete all the initial setup like setting the preferred location, language, and wireless setup. Once done visit the Roku site and create the account by entering the required credentials. You will receive the activation code on the Roku screen. Visit the Roku.com/link and enter the Roku account activation code displayed on the Roku screen. For further details get in touch with our customer support team@ +1-844-489-7600

greyhankin said...

It’s not a tough task to activate Roku. Connecting the hardware is the first step. Visit the signup URL for account creation. Collect the code. Open the new browser to paste the URL, Roku.com/linkand wait for the prompt to enter the activation code. For assistance and to know more about device setup and troubleshooting, reach out to our customer support at the earliest.

greyhankin said...

Being the best streaming service in the US, Roku has a keen track of all your activities using the Roku account that you create. Roku also needs you to complete the Roku.com/link for activating the account. Here are certain things that we assist you on to complete the Roku activation.
For any further queries on Roku com link Activation, feel free to contact our active customer care team, working round the clock at the toll-free number.

crishbone said...

Facebook allows you to send and receive messages and stores the messages in inbox for your reference. However, if you’re not able to see your messages in inbox and therefore want to reach Facebook phone number for support then all you have to do is, try reloading the page and talkie your device near to the Wife source.

Lanedavis said...

Unable to give feedback to Cash App? Call Cash App Phone Number

When it comes to feedback, it is highly essential for introspection and self improvement. That's why, we at Cash App, take your feedback very seriously. So if you’re facing a problem while giving your feedback, then please do call Cash App Phone Number for assistance.

Lanedavis said...

Can’t talk to the Cash App representative on the Cash App Phone Number?

We at Cash App believe in giving our customers unhindered support whenever they face an issue. That’s why, we provide support through email, chat and phone number. However, the phone number gives an automated response as we don’t have any physical representatives. Therefore, if you face any issue call Cash App Phone Number to get the problem fixed.

For Index said...

roku.com/link | roku com link | roku link support | roku activation code

get the best and be the best said...

Build an on-demand taxi-hailing app like UBER with Goappx’s Uber clone script. No coding. Customize the app anyway you like. Go live instantly. Uber Clone Script | Uber Clone App | Uber Clone | Taxi Booking Script | Uber Clone Script free download | uber clone open source download | taxi booking app source code free | Uber Clone app source code free | uber clone app price . Goappx's WhatsApp clone script will help you deploy a powerful serverless instant messaging app like WhatsApp Clone Script | Wechat Clone |WhatsApp Clone| uber for x clone | uber for x clone script | uber for x clone app | On-Demand Services Clone | Urbanclap Clone | Gojek Clone ||wechat clone source code | wechat clone download | whatsapp clone app download | whatsapp clone app source code | whatsapp clone apk latest version | clone whatsapp web apk download

Unknown said...

Do this hack to drop 2lb of fat in 8 hours

Well over 160k women and men are trying a simple and SECRET "liquids hack" to burn 1-2 lbs each and every night in their sleep.

It is very easy and works with everybody.

This is how you can do it yourself:

1) Go grab a glass and fill it up with water half full

2) Proceed to do this proven hack

and you'll be 1-2 lbs thinner the very next day!

Ismail said...

Nice post!! Thanks for sharing...
Interior Design Sketches in Bangalore

John David said...

Check out the recent articles on our webpage to get an idea of Roku.com/link activation, tips to resolve Roku.com link errors and it’s quite informative. Start writing and you will never end up feeling bored
If you are new to Roku, refer the instructions and setup process becomes much easier

sammer mark said...

Wow!!! Awesome post I really love to read your post. If your are looking or facing from printer related issues like www hp com setup, etc. Our team is available for resolve your printer related any issues in the simple way by our expert technician.

sammer mark said...

Awesome post. I simply stumbled upon your weblog and wanted to mention that I have really loved surfing around your blog posts. If you are facing problem regarding HP Printer in Error State. Call our Toll-Free number or visit our website for more details or any information. We are available 24 x 7.

Garmin GPS said...

I know the value of Garmin Express Update and keep reading good content about the same. I found this blog very informative and it increased my understanding about garmin update to a great extent.

Garmin Nuvi said...

I did not know the entire benefits of Garmin Nuvi Update. But since I have read this blog on map update, I have installed all available updates for my garmin device. I must say that this piece of blog has helped me improve navigation experience. I am not suggesting all my friends to read this blog and get Garmin map update with this team.

Antivirus said...

We did not know the all benefits of Panda Antivirus Support Number. I have read this blog on Antivirus I have installed all available updates. I must say that this piece of blog has helped me improving navigation experience. I am not suggesting all my friends to read this blog and get Antivirus Panda Support with this team. You can use Panda Antivirus to get panda help desk update. Be it Norton Internet security, panda, Kaspersky, McAfee, or any other antivirus.

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
Data science Interview Questions
Data Science Course

ravali said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.

data science course

The Qb Payroll said...

Avail best customer experience with QuickBooks Customer Service Phone Number 1-833-780-0086. Our QuickBooks experts offer quality & genuine assistance. FOr More: https://g.page/qb-support-oregon

Aiden Jangra said...

Its really interesting and amazing blog, Its very informative and helpful for me. I appreciated your hard work and skills. HP printer setup Thanks for sharing!

Roku Com Link said...

Thanks for sharing this information with us. I am just surfing on internet and suddenly found this blog. Very Nice blog. Please have a look on my blog which is based on Roku com link settings.

CloudLearn ERP said...

This article contains some of the most informative content I've read in quite some time. The points of this content are clear-cut and engaging. I think much like this writer.
SAP training in Mumbai

Data Science training in Mumbai Best

Best Data Science training in Mumbai

ravali said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!

Correlation vs Covariance

ravali said...

cool stuff you have and you keep overhaul every one of us

Correlation vs Covariance

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression

sammer mark said...

Your posts are really different from others. It contains good blogs Thank you for sharing really informative blogs. If your How to Fix Error During HP Wireless Printer Setup or any other HP printer related issues Call Our Toll-Free number.

Roku Help Number said...

Roku device is the best service in the USA, Do you have an issue with your Roku Device? Get instant solution and support by Roku Helpline Number. We are dealing with a wide range of Roku streaming players and provide a complete solution for Roku devices.call +1-855-879-1273 visit here https://roku-helpnumber.com/

Roku Help Number
Roku Customer Service Number
Roku Technical Support Number

Florahmelda said...

Those ESL assignment writing services have an advantage of hiring the best English language coursework writing service company that is familiar with ESL assignment help services for their English Language Writing Services.

Anirban Ghosh said...

Your article has piqued my interest. This is definitely a thinker's article with great content and interesting viewpoints. I agree in part with a lot of this content. Thank you for sharing this informational material.
SAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata
SAP training institute Kolkata

Dubai Dandb said...

Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

Data Science In Banglore With Placements
Data Science Course In Bangalore
Data Science Training In Bangalore
Best Data Science Courses In Bangalore
Data Science Institute In Bangalore

Thank you..

Technical Support and Services By Experts said...

Thanks for sharing such a good content with your blogs. I really enjoying your blog while reading. if you need any kind of information CONNECT Brother Printer Number
brother printer customer support
brotehr printer drivers

Ronaldo drogba said...

Disneynow.com/activate | My all-time craze is to explore the world of creative writing. My way of thinking is unique, so as my content. As technology keeps growing, my affinity for writing will also keep on growing. I provide my readers with a perfect answer to their search. Get to know more about me and my writing style by visiting my web pages and reading my blogs to get the best solutions.  - Disneynow.com/activate | +1-805-741-1200

get the best and be the best said...

uber for x clone | uber for x clone script | uber for x clone app| On-Demand Services Clone | Urbanclap Clone | Gojek Clone | Uber For Courier | Uber For Freight Clone | Uber For Shipping | Uber for Freight | uber freight vehicle requirements | uber freight tracking | uber freight plus | uber freight vehicle requirements | Uber For Shipping | Uber For Freight Clone | Uber Clone Script
Uber Clone App
Uber Clone
Uber Clone Script free download
Taxi Booking Script
JustEat Clone Script
JustEat Clone
GrubHub clone
Ubereats Clone
Delivery App Clone
Delivery App Clone Script
Postmates clone
Tiktok clone
Musically clone App
Tiktok clone app
Musically clone

get the best and be the best said...

Uber Clone Script
Uber Clone App
Uber Clone
Taxi Booking Script
Uber Clone Script free download
uber clone open source download
uber clone app price
WhatsApp Clone Script
Wechat Clone
WhatsApp Clone
wechat clone source code
wechat clone download
whatsapp clone app download
whatsapp clone app source code
whatsapp clone apk latest version
clone whatsapp web apk download
E-Commerce Clone
E-Commerce Clone Script
Best Buy Clone
Amazon Clone
uber for x clone
uber for x clone script
uber for x clone app
On-Demand Services Clone
Urbanclap Clone
Gojek Clone
JustEat Clone Script
JustEat Clone
GrubHub clone
Ubereats Clone
Delivery App Clone
Delivery App Clone Script
Postmates clone
Tiktok clone
Dubsmash Clone App
Musically clone App
Tiktok clone app
Dubsmash Clone
Musically clone

EXCELR said...


Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.
Data Science Course in Hyderabad

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

CloudLearn ERP said...

This article is worthy of recognition and comment. I found this material attention-grabbing and engrossing. This is well-scripted and highly informative. These views appeal to me. This is how real writing is done. Thank you.
Data Science training in Mumbai
Data Science course in Mumbai
SAP training in Mumbai

Anirban Ghosh said...

If I had to give a prime example of great quality content, this article would be one. It's well-written material that keeps your interest well.
SAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata

priyash said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm
Logistic Regression explained

Anirban Ghosh said...

I would like to thank you for getting my neurons conspicuous with this brilliant article that you have written which contains every potential points which needs to considered on the given topic. Thanks for chipping in such a brilliant writing!
SAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata

carolineangela said...

Hp envy printer 5055 support | It is my utmost desire to explore all the digital products as a whole. Passion doesn't fade away and I've always liked to explore more . Refer my blogs and articles regarding the issues you face with the HP printer | +1-844-485-5080 | Hp envy 5055 keeps going offline

get the best and be the best said...

Uber Clone Script
Uber Clone App
Uber Clone
Taxi Booking Script
Uber Clone Script free download
uber clone open source download
uber clone app price
WhatsApp Clone Script
Wechat Clone
WhatsApp Clone
wechat clone source code
wechat clone download
whatsapp clone app download
whatsapp clone app source code
whatsapp clone apk latest version
clone whatsapp web apk download
E-Commerce Clone
E-Commerce Clone Script
Best Buy Clone
Amazon Clone
uber for x clone
uber for x clone script
uber for x clone app
On-Demand Services Clone
Urbanclap Clone
Gojek Clone
JustEat Clone Script
JustEat Clone
GrubHub clone
Ubereats Clone
Delivery App Clone
Delivery App Clone Script
Postmates clone
Tiktok clone
Dubsmash Clone App
Musically clone App
Tiktok clone app
Dubsmash Clone
Musically clone

get the best and be the best said...

Uber For Freight
uber freight vehicle requirements
uber freight tracking
uber freight plus
Uber For Shipping
Uber For Freight Clone
Periscope Clone
Bigo Clone
Live Streaming Clone
best live streaming clone script
Bigo live / Periscope live clone app
live streaming script
live streaming clone script
Periscope clone script
Periscope live clone app
Tiktok Clone App
Dubsmash Clone App
Musically clone App

aragates said...

Hey there, I'm Ara I work as a Freelancer living in Washington. I read Books a lot that's my hobby too. You can visit my favorite website Channels TV Live | univision.com/activate

Arga Detectives said...

Detectives privados Madrid Asociados es un despacho de investigación privada en Madrid con gran experiencia. Contacta con los mejores detectives privados en Madrid para todo tipo de investigaciones privadas.

detectives privados madrid
agencia detectives Madrid

Thank you..

priyanka said...

very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Logistic Regression explained
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm
Bag of Words Python

طراحی سایت said...

طراحی اپلیکیشن
توریست درمان