lua-users home
lua-l archive

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

Have you looked into using weak tables?

I think they might do the trick.


> -----Original Message-----
> From: 
> [] On Behalf Of 
> Mike Ferenduros
> Sent: Wednesday, October 20, 2004 3:01 PM
> To: Lua list
> Subject: Unlocked refs
> Hello,
> Excuse the newbie question, but...
> The short version is, I think I need to use unlocked refs, but I see 
> they're no longer supported. Is this because there's 
> something else that 
> does the same job?
> The longer version is that I want them to manage userdatas. 
> I'm exposing 
> C objects to Lua via the traditional 
> boxed-pointer-plus-metatable thing. 
> However, these C objects can be deleted leaving Lua with userdatas 
> containing stale pointers. So what I want to do is:
>  - Each Lua-accessible C object contains a reference to the userdata 
> which points to it (or NOREF if there isn't one)
>  - When a script requests a C object, I return the objects existing 
> userdata if there is one, else I create a new one
>  - When a userdata is GCd, I set its C objects reference to NOREF
>  - When a C object is freed, I NULL out the pointer back to it inside 
> its userdata (if it has one).
>  - All functions/methods acting on userdatas check for a NULL 
> object-pointer to detect stale userdatas.
> The problem I'm having is with returning an existing userdata to a 
> script; in order to do this I need a reference to it, which I need to 
> obtain with lua_ref. But in the absence of unlocked refs, 
> this means the 
> userdata will never get GCd.
> I've got a more complicated method working but it seems like 
> the above 
> should be possible. Am I missing something obvious?
> Thanks
> Mike