lua-users home
lua-l archive

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


Thanks Kevin. That's what I'm doing, except I'm not using the macros, rather calling the methods directly.

Am I missing something, or wouldn't it be nice to be able to garbage collect a table from c++ as well as userdata?

The only way to receive a __gc event on a user data is to make it a full
user data with a metatable that contains a __gc metamethod.

A relatively lightweight way to do this is to use Lua's "boxpointer" set
of macros.

<code>
    lua_boxpointer( L, my_pointer);
    lua_newtable( L );
    lua_pushstring( L, "__gc" );
    lua_pushcfunction( L, myGCFunction );
    lua_settable( L, -3 );
    lua_setmetatable( L, -2 );
</code>

<code>
int myGCFunction( lua_State* L )
{
    MyData* my_pointer = ( MyData* ) lua_unboxpointer( L, 1 )

    delete my_pointer;
}
</code>

-Kevin

> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br
> [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of
> Robert Sadedin
> Sent: Monday, October 18, 2004 7:53 PM
> To: lua@bazar2.conectiva.com.br
> Subject: metatable and c++ assistance/thoughts
>
>
> Hi all,
>
> I'm trying to work with objects in both lua and c++, and am
> playing with
> creating a table from c++ with static methods from c++ linked
> into table
> functions, and a pointer to the c++ object stored in the
> table with some
> appropriate name (this, for instance).
> Then, when a table method is called with the : operator, the table is
> passed,  I can extract the pointer, and everything is groovy
> then to call
> the object from c++.
>
> I want to control the garbage collection on the object,
> though, since at
> present it leaks terribly because the garbage collector
> doesn't fire on the
> lightuserdata I'm storing the pointer as.
>
> I can make it a userdata, and specifically create it and add
> a metatable to
> it for __gc, but I'd rather not have to.
>
> Is there anyway I can control destruction of a table (appears
> not from the
> manual)?  Perhaps people can very quickly say I'm simply a
> fool for thinking
> of managing things in the manner I've set out ... ?
>
>
>
> Rob
>