Questions tagged [raii]

Resource Allocation Is Initialization is a pattern that ties lifetime of an allocated object to allocated resources. Resources are allocated when an object is constructed and freed when the object is destroyed.

Many kinds of resources require a proxy in the application to represent accessibility and control. In an object oriented language that has explicit construction and destruction, you can control the lifetime of access to the resource by tying it to the object that acts as its proxy.

Here is a good summary of RAII that gives examples and provides some discussion.

13 questions
40
votes
8 answers

Disadvantages of scoped-based memory management

I really like scope-based memory management (SBMM), or RAII, as it is more commonly (confusingly?) referred to by the C++ community. As far as I know, except for C++ (and C), there's no other mainstream language in use today that makes SBMM/RAII…
Paul
  • 2,134
  • 3
  • 18
  • 18
33
votes
5 answers

Why can't Java/C# implement RAII?

Question: Why can't Java/C# implement RAII? Clarification: I am aware the garbage collector is not deterministic. So with the current language features it is not possible for an object's Dispose() method to be called automatically on scope exit. But…
mike30
  • 2,788
  • 2
  • 16
  • 19
7
votes
4 answers

Is the RAII wrapper a good idea for this C transactions API or should I stick to the C style?

Consider the following C API: void BeginTransaction(State *s); void AddToTransaction(State *s, Object *value); void CommitTransaction(State *s); void Foo(State *s, Object *value) { BeginTransaction(s); AddToTransaction(s, value); …
Andrew Sun
  • 251
  • 2
  • 9
6
votes
4 answers

RAII Idiom Extension Suggestions

RAII is by far the most useful idiom in c++. However there seem to be two common pitfalls associated with it that I believe need to be formally addressed. Failure to release a resource in the destructor and resource invalidation prior to…
5
votes
1 answer

Is there a way to use RAII techniques in COM?

I am caught between two conceptual models of how to construct objects, and am having trouble working out what standard I want to adopt. I prefer to program in .NET and often try to use RAII and read-only properties to make immutable objects with…
Mike
  • 649
  • 4
  • 11
4
votes
2 answers

Preferable design of scope guard in C++

Recently, I came across an issue about the design of scope guard. A scope guard invokes a supplied function object (usually performs cleanup procedures) upon exiting the enclosing scope. The current design is as follows: #define…
Lingxi
  • 155
  • 1
  • 7
3
votes
3 answers

Possible alternatives to copy constructors

In my C++ project I am relying on some libraries that do memory management for me. I make wrapper classes, for ease of use and memory safety, for example the class below. Note that this is a much simplified example, to demonstrate my…
Oebele
  • 215
  • 2
  • 8
2
votes
2 answers

How does this pseudo-RAII implementation allow for a scoped lock in C#?

For the concurrency programs I have been writing in C#, my locks/synchronization tend to follow this pattern: try { Monitor.Enter(locker); // critical region } finally { Monitor.Exit(locker); } This is a classic pattern I've discovered…
Snoop
  • 2,718
  • 5
  • 24
  • 52
2
votes
2 answers

When to actually use RAII?

I understand the concept of RAII: Use the destructor as a means to free resources, such as memory, or closing file handles/database connections. Coming from a Java background this was actually rather easy to understand because of the similarities…
KUSH42
  • 131
  • 4
2
votes
1 answer

How often should RAII be used?

I've been attempting to learn C++, but it is famously plagued by bad tutorials. I learned about a clever little trick called RAII (Resource Acquisition is Initialization), where one wraps a heap variable in an object placed on the stack. One would…
2
votes
7 answers

What if any languages treat undisposed resources as an error?

I've seen lots of code like the following example. It's in Python, but the same mistake is made in all languages with managed resources: f = open('foo.txt', 'rb') for line in f: print line That's it. The error is that close(f) wasn't called so the…
1
vote
3 answers

C API in C++ with RAII, two alternatives to implement error handling (Exceptions)

I have an API written in C, which produces a result by returning a pointer to allocated memory. For using it with C++ (C++11) I've wrapped the function calls in objects, which keep the result in a std::shared_ptr. So far so good. However, the C…
jchnkl
  • 13
  • 3
0
votes
1 answer

Managing the disposal of network connections

I am writing a class -- let's call it MessageSender -- that needs to perform operations over the network. It basically does these things: Take some configuration establish a connection send stuff If we ignore the cleanup of any resources, this…
Felk
  • 184
  • 7