lua-users home
lua-l archive

Re: GC and userdata objects

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


> Hi. I have tried using GC step before but did not quite succeed in
> resolving similar issues but that could have been due to my lack of
> understanding. Is there an example of how it should be used?
Memory managed by Lua:
 void *u = lua_newuserdata(L, Nbytes);
 /* no other action required; Lua already knows about the allocation */
Memory managed outside Lua:
 void **u = lua_newuserdata(L, sizeof(void*));
 /* code to set metatable with __gc for the new userdata */
 luaL_getmetatable(L, whatever);
 lua_setmetatable(L, -2);
 /* allocate memory for the new userdata */
 *u = malloc(Nbytes);
 if (*u == NULL) error ...;
 /* Lua does not know about the allocation */
 lua_gc(L, LUA_GCSTEP, Nbytes / 1024); /* tell Lua about it */
-- Roberto

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