Skip to main content

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.

Filter by
Sorted by
Tagged with
0 votes
2 answers
210 views

Is it considered a code smell to not delete static pointers of a singleton?

I have a singleton that needs to be initialized at the start of the program and it will live on until the end of the program. As is usual in this pattern, a function initializing the singleton creates ...
Reverent Lapwing's user avatar
0 votes
1 answer
286 views

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 ...
Felk's user avatar
  • 194
2 votes
2 answers
545 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(...
Snoop's user avatar
  • 2,738
2 votes
2 answers
2k 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 ...
KUSH42's user avatar
  • 131
7 votes
4 answers
2k 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) { ...
Andrew Sun's user avatar
6 votes
4 answers
617 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 ...
Aaron Albers's user avatar
4 votes
2 answers
4k views

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 ...
Lingxi's user avatar
  • 155
3 votes
3 answers
1k 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 ...
Oebele's user avatar
  • 215
2 votes
1 answer
2k 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 ...
CamelCaseCode's user avatar
2 votes
7 answers
274 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....
Gaslight Deceive Subvert's user avatar
5 votes
1 answer
382 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-...
Mike's user avatar
  • 649
40 votes
8 answers
7k 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 ...
Paul's user avatar
  • 2,174
1 vote
3 answers
2k 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:...
jchnkl's user avatar
  • 13
33 votes
5 answers
11k 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() ...
mike30's user avatar
  • 2,808