lua-users home
lua-l archive

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


I have a problem with luaSQL(1.0) (using Lua 4.0.1) that I
cannnot solve and I would appreciate any assistance.

The code I have creates an lua table for ODBC environment,
connection and cursor handles. i.e. hENV, hDBC , hSTMT

I wish to ensure that all handles are closed and all memory
is deallocated when lua_close is called. I also want the
lua garbage collection to close any loose statement(cursor)
handles. However, I dont want statement handles closed when
I am fetching data from them.


The typical lua application that is being written reads like

env=openenv();
conn=env:open();

while (get a user request())
  stmt=conn:exec(...)
  while ()
    result=stmt:fetch()
    DoSomeThingWithTheResult()
  end
  stmt:close()
end

The garbage collection cycle can execute while in
DoSomeThingWithTheResult() which trashes the stmt handle(s).

How do I stop the gc during the DoSomeThingWithTheResult() ?


Finally, the following two C code fragments are used in luaSQL,
which approach is better?
1)
    stmt_data *sdata = (stmt_data *) malloc(sizeof(stmt_data));
    sdata->stmt_tag = lua_newtag(L);
    lua_pushusertag(L, (void *)sdata, sdata->stmt_tag);
    lua_pushcclosure(L, stmt_gcmethod, 1);
    lua_settagmethod(L, sdata->stmt_tag, "gc");
/* gc method closes handle and does free(sdata) */

2)

  lua_settagmethod(L, env->cursor_tag, "gc");
   ... other code

  Cursor_data *cursor = (Cursor_data *)lua_newuserdata(L,
sizeof(Cursor_data));
  /* Change tag. Otherwise we will create another userdata when we push it.
*/
  lua_settag(L, env->cursor_tag);
  lua_pop(L, 1);
  lua_pushusertag(L, conn, env->conn_tag);
  cursor->conn = lua_ref(L, 1);
  ... other code
/* gc method closes handle, does  lua_unref(L, cursor->conn);
   and presumably relies on gc to clean up the cursor userdata */

David Burgess