lua-users home
lua-l archive

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

The following extension and Lua program crash both Lua 5.1 and Lua
5.2.  From an strace it appears that the extension library is called
into after it was unloaded.  Perhaps this is a GC-related bug where
the loaded library is collected prematurely?


#include "lauxlib.h"

#if LUA_VERSION_NUM == 501
#define newlib(L, name, l) luaL_register(L, name, l)
#elif LUA_VERSION_NUM == 502
#define newlib(L, name, l) luaL_newlib(L, l)

static int nop(lua_State *L) { return 0; }

static const struct luaL_Reg funcs[] = {
  {"nop", nop},

int luaopen_ext(lua_State *L) {
  newlib(L, "ext", funcs);
  return 1;


if _VERSION >= 'Lua 5.2' then
  function atexit(fn)
    setmetatable({}, { __gc = fn })
  function atexit(fn)
    getmetatable(newproxy(true)).__gc = fn

atexit(function() func() end)

local ext = require "ext"

function func() ext.nop() end