lua-users home
lua-l archive

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


Hello lua land,

I have a question about using lua_error in C++.  We have been
using lua in C++ but not with constructors/destructors.  I have
noticed that there is a longjmp() in lua_error.  Presumably
(at least according to gcc and MSVC++) this will prevent any
of the objects in the stack frames from having their destructors
called on them.  This presents a problem, as I allocate objects
on the stack that take up heap space and therefore this will
result in a memory leak.

Is there any workaround for it, and perhaps maybe in C++ this
should be handled using the exception mechanism?  Perhaps the
lua_call should be encapsulated in a try {} catch block?  The
only strategy I have been able to devise so far is:

void function_from_lua()
{
  int parm = luaL_check_number(1);
  int iserror = 0;
  {
    object o(myconstructordata);
 
    if (do_the_work_fails()) {
       iserror = 1;
    } 
  }  /* end of block ensures destructor executes */
  if (iserror)
    lua_error("Something bad happened");
}

It is doable, but seems kind of clunky.  Perhaps something
better exists?

Thank you

Dan Marks
dmarks@uiuc.edu