Re: Scope variable pattern
Re: Scope variable pattern
- Subject: Re: Scope variable pattern
- From: Erik Buck <email@hidden>
- Date: Fri, 11 May 2007 11:44:46 -0700 (PDT)
I am certainly familiar with the C++ idiom of Resource Acquisition Is Initialization (RIAA) at least as stated by Bjarne Stroustrup < http://www.research.att.com/~bs/bs_faq2.html#finally>. I see it all the time, and in fact the C++ standard library cant be fully used without also using RIAA.
However, I am usually most concerned with code readability particularly for people who are not intimately familiar with the system.
It seems that RIAA as proposed by Stroustrup serves three main purposes:
1) Assure cleanup no matter how a function exits because C++ guarantees that destructors will be called even when an exception is thrown.
2) Eliminate the possibility that a programmer will forget to clean up before leaving scope.
3) Reduce the need for a finally block when dealing with exceptions.
Two of the three purposes are related to C++ exceptions and that may be a reason I harshly evaluate this particular idiom (which I believe is unique to C++) . C++ exceptions themselves are problematic and doubly so if Cocoa is mixed in.
Proper support for C++ exceptions is relatively recent. C++ exceptions were not fully supported in GCC 2.8.1 and were still buggy in GCC 2.95. C++ exceptions only became reliable in GCC 2.95.3 (March 16, 2001). I see a lot of code that is older than that. I might also add that Apple used and supplied GCC 2.95.2 for use with Mac OS X version 10.1. Visual C++ and Metrowerks C++ were late with exception support too.
Of critical importance is the fact that Objective-C exceptions when used from Objective-C++ do not interoperate with C++ exceptions as of Mac OS X version 10.4 (Tiger). This means you cannot @throw an exception from Objective-C and catch it in C++, or vice versa (i.e., throw ... @catch).
To me, It seems like a path to nowhere to simultaneously use Cocoa and a C++ idiom intended to improve code behavior when C++ exceptions are thrown.
In summary, I judge that the reducing code readability particularly for people who are not intimately familiar with the system is too high a price to pay to avoid one extra line of intention revealing code each time a local autorelease pool is needed. Most Cocoa programs never need an explicitely created local autorelease pool. My experience makes me shy away from C++ exceptions entirely when also using Cocoa, so I personally dont give much weight to the argument that RIAA makes exception handling better in the case being discussed.
All of this is just my opinion. As always, feel free to disagree. I have never tried to use Cocoa extensively from a primarily Carbon C++ application, and I admit that in such cases the proposed use of RIAA for managing Cocoa autorelease pools might be the most elegant approach. If so, that just makes be more afraid of mixing Cocoa into Carbon C++ based applications
but thats just me.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden