lua-users home
lua-l archive

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


Matt,

I'm glad that you implemented the same workaround that I thought of!  (I'm
using boost::shared_ptr instead of COM)

Don't forget to remove the key-value pair after the COM object is gone... or
you'll start to leak registry memory.  Does your usedata's destructor clean
up its own registry entry?

-Erik



-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Matt Campbell
Sent: Monday, January 08, 2007 12:40 PM
To: Lua list
Subject: Re: userdata as table keys

To address this problem, I use a cache table stored in the Lua registry. 
  In this table, the keys are light userdata (the raw pointers, to COM 
objects in my case), and the values are the full userdata.  The cache 
table has a metatable whose __mode field is "v", so the values in the 
cache table are weak reference.  Then when a C function is about to wrap 
an object, it first checks the cache table for an existing wrapper.  If 
there is one, that is returned; otherwise, a new full userdatum is 
created and returned, and the appropriate entry is added to the cache 
table.  This way, userdata can serve as table keys, and I don't even 
have to implement an __eq metamethod, unless the object being wrapped 
has its own semantics for equality beyond comparing pointers.

I hope this helps.
-- 
Matt Campbell
Lead Programmer
Serotek Corporation
www.freedombox.info
"The Accessibility Anywhere People"