lua-users home
lua-l archive

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


Hi!

Thank you for your answer!

I am keeping direct pointers to Table, Closure, TString and Udata at various places in the C++ code and want them to be alive until C++ code does not need them anymore. Standard practice is to use the registry, but in my case "pinning" object in memory would be a much cleaner solution. Some kind of "Don't try this at home" action :)

I have tried turning on the FIXEDBIT bit on a table value, but it did not give any effect (table is collected at the first GC run after reference to table has been removed from the Lua stack).

Can you please take a look at my test code, maybe I am just missing something?
I want both parent and child table to survive second pack of GC calls (after reference has been popped from the stack).


#include "lua.h"
#include "lauxlib.h"
#include "lgc.h"
#include "ltable.h"
#include "lapi.h"
#include <stdio.h>

static int collectParent(lua_State * L) { printf("collectParent!\n"); return 0; }
static int collectChild(lua_State * L) { printf("collectChild!\n"); return 0; }

int main(int argc, char ** argv)
{
        lua_State * L = luaL_newstate();

        /* create parent table and put it on stack. */
        Table * table = luaH_new(L);
        sethvalue(L, L->top, table);
        api_incr_top(L);

        /* attach finalizer 'collectParent' to the parent table. */
        lua_newtable(L);
        lua_pushcfunction(L, collectParent);
        lua_setfield(L, -2, "__gc");
        lua_setmetatable(L, -2);

        /* create child table */
        lua_newtable(L);

        /* attach finalizer 'collectChild' to the child table. */
        lua_newtable(L);
        lua_pushcfunction(L, collectChild);
        lua_setfield(L, -2, "__gc");
        lua_setmetatable(L, -2);

        /* store child table in the parent table */
        lua_setfield(L, -2, "child");

        /* at this point we have a parent table on the top of the stack.
           parent table is the only reference to the child table. */

        /* Let's try to lock object in memory */
        l_setbit(table->marked, FIXEDBIT);

        printf("--1--\n");
        luaC_fullgc(L, 0);
        luaC_fullgc(L, 0);
        luaC_fullgc(L, 0);
        luaC_fullgc(L, 0);
        luaC_fullgc(L, 0);

        printf("--2--\n");

        lua_pop(L, 1);                  /* remove parent table reference from the stack. */

        luaC_fullgc(L, 0);
        luaC_fullgc(L, 0);
        luaC_fullgc(L, 0);
        luaC_fullgc(L, 0);
        luaC_fullgc(L, 0);

        printf("--3--\n");

        lua_close(L);

        return 0;
}



On Mon, Dec 9, 2013 at 3:57 PM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
I wonder why you want to keep alive objects thara have no references to them.
They'll be forever inaccessible except from the chain of values inside C.
Note that it does make sense to keep strings alive because they are interned
and so can resurrected by value. Not so for tables and closures.




--
С уважением,
Запольнов Николай