lua-users home
lua-l archive

Userdata memory is not freed

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


I've got a case where the memory occupied by a userdatum
is not freed, even though Lua calls its __gc method.
I'm failing to understand that, any help is appreciated!
(Lua 5.1.2)
testlib.c and test.lua follow:
/* testlib.c */
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
static int new_udata (lua_State *L) {
 int size = luaL_optinteger (L, 1, 32768);
 lua_newuserdata (L, size);
 lua_pushvalue(L, LUA_ENVIRONINDEX);
 lua_setmetatable (L, -2);
 return 1;
}
static int udata_gc (lua_State *L) {
 (void)L;
 printf("inside __gc\n");
 return 0;
}
static const luaL_reg meta[] = {
 { "__gc", udata_gc },
 { NULL, NULL }
};
static const luaL_reg lib[] = {
 { "new", new_udata },
 { NULL, NULL }
};
LUALIB_API int luaopen_testlib (lua_State *L) {
 /* create a new function environment */
 lua_newtable (L);
 lua_pushvalue (L, -1);
 lua_replace (L, LUA_ENVIRONINDEX);
 luaL_register (L, NULL, meta);
 /* register functions */
 luaL_register (L, "testlib", lib);
 return 1;
}
-- test.lua
require "testlib"
print("1:", collectgarbage"count") --> 22 KB
testlib.new(2^20)
print("2:", collectgarbage"count") --> 1046 KB
collectgarbage"collect"
print("3:", collectgarbage"count") --> 1044 KB
--
Shmuel

AltStyle によって変換されたページ (->オリジナル) /