raii

Does ScopeGuard use really lead to better code?

I came across this article written by Andrei Alexandrescu and Petru Marginean many years ago, which presents and discusses a utility class called ScopeGuard for writing exception-safe code. I'd like to know if coding with these objects truly leads to better code or if it obfuscates error handling, in that perhaps the guard's callback wou...

Is there a better deterministic disposal pattern than nested "using"s in C#?

In C#, if I want to deterministically clean up non-managed resources, I can use the "using" keyword. But for multiple dependent objects, this ends up nesting further and further: using (FileStream fs = new FileStream("c:\file.txt", FileMode.Open)) { using (BufferedStream bs = new BufferedStream(fs)) { using (StreamReade...

How do you pronounce RAII?

RAII (=resource acquisition is initialization) is used heavily in C++. I'm not a native speaker. So, how do you pronounce it? ...

Why is it wrong to use std::auto_ptr<> with STL containers?

Why is it wrong to use std::auto_ptr<> with STL containers? ...

throwing exceptions out of a destructor

Most people say never throw an exception out of a destructor - doing so results in undefined behavior. Stroustrup makes the point that "the vector destructor explicitly invokes the destructor for every element. This implies that if an element destructor throws, the vector destruction fails... There is really no good way to protect agains...

RAII vs. exceptions

The more we use RAII in C++, the more we find ourselves with destructors that do non-trivial deallocation. Now, deallocation (finalization, however you want to call it) can fail, in which case exceptions are really the only way to let anybody upstairs know of our deallocation problem. But then again, throwing-destructors are a bad idea b...

Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

Does C++ support 'finally' blocks? What is the RAII idiom? What is the difference between C++'s RAII idiom and C#'s 'using' statement? ...

Do programmers of other languages, besides C++, use, know or understand RAII?

I've noticed RAII has been getting lots of attention on Stackoverflow, but in my circles (mostly C++) RAII is so obvious its like asking what's a class or a destructor. So I'm really curious if that's because I'm surrounded daily, by hard-core C++ programmers, and RAII just isn't that well known in general (including C++), or if all thi...

Why is there no RAII in .NET?

Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or .NET's using construct) seems to be markedly inferior. I see why in Java there is no suppor...

RAII in Java... is resource disposal always so ugly ? (was: I had a dream ?)

Hi all, I just played with Java file system API, and came down with the following function, used to copy binary files. The original source came from the Web, but I added try/catch/finally clauses to be sure that, should something wrong happen, the Buffer Streams would be closed (and thus, my OS ressources freed) before quiting the funct...

RAII in Ruby (Or, How to Manage Resources in Ruby)

I know it's by design that you can't control what happens when an object is destroyed. I am also aware of defining some class method as a finalizer. However is the ruby idiom for C++'s RAII (Resources are initialized in constructor, closed in destructor)? How do people manage resources used inside objects even when errors or exception...

CUDA: Wrapping device memory allocation in C++

Hello, I'm starting to use CUDA at the moment and have to admit that I'm a bit disappointed with the C API. I understand the reasons for choosing C but had the language been based on C++ instead, several aspects would have been a lot simpler, e.g. device memory allocation (via cudaMalloc). My plan was to do this myself, using overloade...

shared_ptr: what's it used for

Hi all, I make a lot of use of boost::scoped_ptr in my code and it is great but I'm currently working with software that uses shared_ptr all over the place and I'm wondering if I'm missing something. AFAIK a shared_ptr is only useful if different threads are going to be accessing the same data and you don't know what order the threads...

Implementing RAII in pure C?

Is it possible to implement RAII in pure C? I assume it isn't possible in any sane way, but perhaps is it possible using some kind of dirty trick. Overloading the standard free function comes to mind or perhaps overwriting the return address on the stack so that when the function returns, it calls some other function that somehow relea...

Are there cases where a "finally" construct would be useful in C++?

Bjarne Stroustrup writes in his C++ Style and Technique FAQ, emphasis mine: Because C++ supports an alternative that is almost always better: The "resource acquisition is initialization" technique (TC++PL3 section 14.4). The basic idea is to represent a resource by a local object, so that the local object's destructor will release th...

RAII and smart pointers in C++

In practice with C++, what is RAII, what are smart pointers, how are these implemented in a program and what are the benefits of using RAII with smart pointers? ...

A Question On Smart Pointers and Their Inevitable Indeterminism

I've been extensively using smart pointers (boost::shared_ptr to be exact) in my projects for the last two years. I understand and appreciate their benefits and I generally like them a lot. But the more I use them, the more I miss the deterministic behavior of C++ with regarding to memory management and RAII that I seem to like in a prog...

Good or Bad C++ Idiom - Objects used purely for constructor/destructor?

I have a few classes which do nothing except in their constructors/destructors. Here's an example class BusyCursor { private: Cursor oldCursor_; public: BusyCursor() { oldCursor_ = CurrentCursor(); SetCursor(BUSY_CURSOR); } ~BusyCursor() { SetCursor(oldCursor_); } } // example of use...

Does Java support RAII/deterministic destruction?

It's been at least 5 years since I worked with Java, and back then, any time you wanted to allocate an object that needed cleaning up (e.g. sockets, DB handles), you had to remember to add a finally block and call the cleanup method in there. By contrast, in C++ (or other languages where object lifetimes are deterministic, e.g. Perl), t...

Smart pointers with a library written in C

I'm using C++ with the OpenCV library, which is a library image-processing although that's not relevant for this question. Currently I have a design decision to make. OpenCV, being a C library, has its data structures (such as CvMat) declared as structs. To create them, you use functions like cvCreateMat, and to release them, you use f...