lua-users home
lua-l archive

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


On Mon, Oct 16, 2006 at 03:13:20PM -0700, Sam Roberts wrote:
(snip userdata 101 :)
> This may be tedious, but it is easy to be confident its "correct". C++

It's easy in the sense that C deallocation is easy, though; it's
open to lurking mistakes.  For example,

{
    const string &fn = SArg(1); // actually owned and freed by lua
    file f;
    f.open(fn);
}

is probably correct, and

    file f;
    const string &fn = SArg(1);
    f.open(fn);

is incorrect: SArg might fail, leaking f.  This would compile and run
without complaint; the memory leak is hidden most of the time, and takes
close examination to find, just as in the C equivalent:

    file *f = new_file();
    const char *fn = luaL_checkstring(L, 1);
    file_open(fn);

That's why it's hard, in my mind, to be confidence of correctness.  In the
absense of longjmp, C++ does a lot to prevent this class of errors.

("file" could be wrapped in a userdata, too, and then the order wouldn't
matter, but that could lead to a whole lot of classes needing wrapping,
and there would still be no warning flags if a class needed to be wrapped
and was not.)

I guess it's just a tough balancing act between Lua wanting exception-
like error handling, concise and correct binding code, performance, and
"acceptable memory leaks".

> > > Its hard to have the benefits of C++ automatic resource freeing with
> > > exceptions, without paying the costs for exceptions!
> > 
> > Not at all.
> 
> What does that mean?
> 
> If you have way of getting automatic resource freeing across longjmp
> without paying the cost for exceptions, why does this thread exist?

"automatic resource freeing with exceptions" sounded like exceptions were
an inseparable part of automatic resource freeing, but they're not.  I use
C++'s automatic resource freeing, without using exceptions at all.

-- 
Glenn Maynard