raii

Is shared ownership of objects a sign of bad design?

Hi all, Background: When reading Dr. Stroustrup's papers and FAQs, I notice some strong "opinions" and great advices from legendary CS scientist and programmer. One of them is about shared_ptr in C++0x. He starts explaining about shared_ptr and how it represents shared ownership of the pointed object. At the last line, he says and I quo...

How to solve a problem in using RAII code and non-RAII code together in C++?

We have 3 different libraries, each developed by a different developer, and each was (presumably) well designed. But since some of the libraries are using RAII and some don't, and some of the libraries are loaded dynamically, and the others aren't - it doesn't work. Each of the developers is saying that what he is doing is right, and ma...

RAII possible in Java?

http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization Is this design pattern possible in Java? If so, how? If not, why not? Thanks! ...

Is there a language with RAII + Ref counting that does not have unsafe pointer arithmetric?

RAII = Resource Acuquisation is Initialitazation Ref Counting = "poor man's GC" Together, they are quite powerful (like a ref-counted 3D object holding a VBO, which it throws frees when it's destructor is called). Now, question is -- does RAII exist in any langauge besides C++? In particular, a language that does not allow pointer arit...

Is a C++ destructor guaranteed not to be called until the end of the block?

In the C++ code below, am I guaranteed that the ~obj() destructor will be called after the // More code executes? Or is the compiler allowed to destruct the obj object earlier if it detects that it's not used? { SomeObject obj; ... // More code } I'd like to use this technique to save me having to remember to reset a flag at the e...

RAII in Scheme?

Is there anyway to implement Resource Acquisation is Initialization in Scheme? I know that RAII does not work well in GC-ed languages (since we have no idea whe the object is destroyed). However, Scheme has nice things like continuations, dynamic-wind, and closures -- is there a way to use some combination of this to implement RAII? If...

Is it abusive to use IDisposable and "using" as a means for getting "scoped behavior" for exception safety?

Something I often used back in C++ was letting a class A handle a state entry and exit condition for another class B, via the A constructor and destructor, to make sure that if something in that scope threw an exception, then B would have a known state when the scope was exited. This isn't pure RAII as far as the acronym goes, but it's a...

Any RAII template in boost or C++0x

Is there any template available in boost for RAII. There are classes like scoped_ptr, shared_ptr which basically work on pointer. Can those classes be used for any other resources other than pointers. Is there any template which works with a general resources. Take for example some resource which is acquired in the beginning of a scope ...

Using RAII with a character pointer.

I see a lot of RAII example classes wrapping around file handles. I have tried to adapt these examples without luck to a character pointer. A library that I am using has functions that take the address of a character pointer (declared like get_me_a_string(char **x)). These functions allocate memory for that character pointer and leave ...

Unit Testing Private Method in Resource Managing Class (C++)

I previously asked this question under another name but deleted it because I didn't explain it very well. Let's say I have a class which manages a file. Let's say that this class treats the file as having a specific file format, and contains methods to perform operations on this file: class Foo { std::wstring fileName_; public: ...

What is meant by Resource Acquisition is Initialization (RAiI)?

What is meant by Resource Acquisition is Initialization (RAiI)? www.technical-interview.com ...

RAII and C++ STL

I have a case where I wish to store a list of resources in a std::vector. As I see it, my options are as follows: Give my resource a default constructor Store them as heap objects (and wrap them in a shared pointer) Option 1 makes it possible to construct invalid resources and option 2 forces me to use the heap. Am I missing any opt...

What happens when we combine RAII and GOTO ?

I'm wondering, for no other purpose than pure curiosity (because no one SHOULD EVER write code like this!) about how the behavior of RAII meshes with the use of Goto (lovely idea isn't it). class Two { public: ~Two() { printf("2,"); } }; class Ghost { public: ~Ghost() { printf(" BOO! "); } }; vo...

C/C++ macro/template blackmagic to generate unique name.

Macros are fine. Templates are fine. Pretty much whatever it works is fine. The example is OpenGL; but the technique is C++ specific and relies on no knowledge of OpenGL. Precise problem: I want an expression E; where I do not have to specify a unique name; such that a constructor is called where E is defined, and a destructor is call...

Is this a good way to manage initializations of COM?

Hello everyone :) I'm very new to anything involving Component Object Model, and I'm wondering if this method of managing calls to CoInitalize/CoUninitalize makes sense: COM.hpp: #pragma once namespace WindowsAPI { namespace ComponentObjectModel { class COM { COM(); ~COM(); public: static void Setup(); }; }} COM.cpp: ...

What wrapper class in C++ should I use for automated resource management?

I'm a C++ amateur. I'm writing some Win32 API code and there are handles and weirdly compositely allocated objects aplenty. So I was wondering - is there some wrapper class that would make resource management easier? For example, when I want to load some data I open a file with CreateFile() and get a HANDLE. When I'm done with it, I sho...

resource acquisition is initialization "RAII"

in the example below class X { int *r; public: X() { cout << "X is created"; r = new int[10]; }; ~X() { cout<< "X is destroyed"; delete [] r; }; }; class Y { public: Y() { X x; throw 44; }; ~Y() { cout << "Y is destroyed"; }; }; I got this ...

How can I automatically release resources RAII-style in Perl?

Say I have a resource (e.g. a filehandle or network socket) which has to be freed: open my $fh, "<", "filename" or die "Couldn't open filename: $!"; process($fh); close $fh or die "Couldn't close filename: $!"; Suppose that process might die. Then the code block exits early, and $fh doesn't get closed. I could explicitly check for er...

Destructors not called when native (C++) exception propagates to CLR component

We have a large body of native C++ code, compliled into DLLs. Then we have a couple of dlls containing C++/CLI proxy code to wrap the C++ interfaces. On top of that we have C# code calling into the C++/CLI wrappers. Standard stuff, so far. But we have a lot of cases where native C++ exceptions are allowed to propagate to the .Net wor...

Having a destructor take different actions depending on whether an exception occurred

I have some code to update a database table that looks like try { db.execute("BEGIN"); // Lots of DELETE and INSERT db.execute("COMMIT"); } catch (DBException&) { db.execute("ROLLBACK"); } I'd like to wrap the transaction logic in an RAII class so I could just write { DBTransaction trans(db); // Lots of DELETE ...