lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm not backing any horse in this particular race, but I think about "resources" quite a bit, and the absence of RAII in other languages always makes me feel uneasy.

On 23 November 2015 at 23:23, Coda Highland <chighland@gmail.com> wrote:

But RAII doesn't
magically free you from cleanup boilerplate; it just moves it from the
point of use to the point of declaration. Every class you create has
to explicitly include its own cleanup code[...]

It frees the *caller* from boilerplate code.  The destructor only has to be written once and it is written by the library author, who is hopefully in the best position to do so correctly.  Compare this with requiring every user, at *every point of use* to remember to "using" (C#) or worse still to "finally" (Java, etc) call call close()... or was it release(), or deinit(), or something more complex?

The worst part is that things usually appear to work if the caller forgets to protect themselves.  Taking filehandles as the textbook example, it's far easier to forget to "using" or "finally" than it is to abuse a std::ifstream in order to leak the underlying resource.
 
C++ eventually realized this
headache and added a finally construct to the language

Is that a Visual Studio thing?  I'm sure it's not standard.

Peter