Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

2
votes
2answers
288 views

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(...
2
votes
2answers
840 views

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 ...
7
votes
4answers
922 views

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) { ...
6
votes
4answers
482 views

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 ...
2
votes
2answers
2k views

Preferable design of scope guard in C++

Recently, I come 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 ...
3
votes
3answers
874 views

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 ...
2
votes
1answer
1k views

How often should RAII be used? [closed]

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 ...
2
votes
7answers
262 views

What if any languages treat undisposed resources as an error? [closed]

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....
4
votes
1answer
254 views

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-...
37
votes
8answers
6k views

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 ...
1
vote
3answers
1k views

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:...
28
votes
5answers
7k views

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