[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: GC and userdata objects
- From: Roberto Ierusalimschy <roberto@...>
- Date: Thu, 12 Apr 2018 16:56:00 -0300
> 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