views:

165

answers:

5

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 arithmetric / buffer overflows?

Thanks!

+2  A: 

D has RAII but still has pointer arithmetic :( BUT, you don't really have to use it. Please note getting D to work was a pain in the butt for me so IM JUST SAYING.

Buttink
D's "safe mode" has *no* pointer arithmetic. What it really lacks is Ref counting. It only has rich man's GC :p.
KennyTM
+2  A: 

Perl 5 has ref counting and destructors that are guaranteed to be called when all references fall out of scope, so RAII is available in the language, although most Perl programmers don't use the term.

And Perl 5 does not expose raw pointers to Perl code.

Perl 6, however, has a real garbage collector, and in fact allows the garbage collector to be switched out; so you can't rely on things being collected in any particular order.

I believe Python and Lua use reference counting.

Max Lybbert
CPython uses reference counting plus an optional cycle-detecting garbage collector. Jython and IronPython use the underlying platform's garbage collectors, which are not reference-count based.
ephemient
+2  A: 

perl, python (C), php, and tcl are reference counted and have mechanisms to destroy an object once its reference count goes to zero, which can happen as soon as a variable goes out of scope. built-in types are released automatically. user-defined classes have a way to define a destructor that will get called upon release.

there are some edge cases: global variables might not be released until the end and circular references may not be released until the end (though php has recently implemented a gc that handles this case and python 2 added a cycle detector).

jspcal
+2  A: 

Python (the standard CPython, not variants like Jython, Unladen Swallow and IronPython) uses reference counting for its objects.

With that, it also has RAII and (mostly) deterministic garbage collection. For example, this is supposed to work deterministically closing files:

def a():
   fp = open('/my/file', 'r')
   return fp.read()

Note fp.close() is never called. As soon as fp goes out of scope, the object should be destroyed. However, there are some cases where deterministic finalization is not guaranteed, such as in:

  • Something throws an exception and the traceback is currently being handled, or a reference is kept to it (note sys.last_traceback keeps the last traceback)
  • Cyclic references exist in an object, causing the reference count to not go to zero

Therefore, while python theoretically has deterministic finalization, it is better to explicitly close any resources where it's possible an exception (like IOError or the like) could cause the object to remain live.

Crast
Wouldn't it be better to write `with open('/my/file', 'r') as fp: return fp.read()`?
ephemient
As discussed in the other python answer, that's not RAII; it's just syntactic sugar for a try/finally that closes it. But yes, it's better python in that you will close the file even in exception conditions, assuming you're running python 2.6+ or 2.5 with the `__future__.with_statement` imported. Also, this will work for garbage-collected pythons such as Jython/Unladen Swallow/IronPython
Crast
+3  A: 

While not exactly RAII, Python has the with statement and C# has the using statement.

R Samuel Klatchko
Those are exactly what RAII is not: they put the burden on the caller (ie everywhere) rather than the callee (written once for all).
Matthieu M.
@Matthieu - I agree that they put the burden on the caller. But to me they still feel RAII-like as they have the property that uninitialization does not require a separate call.
R Samuel Klatchko