lua-users home
lua-l archive

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


Hi,

I have a question about what has been discussed on your list before: how
can I invalidate userdata references pointing to C++ objects from C++
side, i.e. I would like to get a runtime error on Lua side when
accessing C++ objects after they are destroyed on the C++ side.

>From what I have read here the preferred way to do this is to have a C++
pointer (as lightuserdata) to full userdata mapping table. When
destroying the object I would do a lookup into this table, somehow mark
the userdata invalid so I can generate an error on all accesses from
that point and remove the entry from the mapping table.

Now I am using tolua++ to map the C++ objects to Lua which seems to make
things a bit more complicated as I do not yet fully understand its inner
workings. tolua++ creates a 'ubox' for each type, in a global table
indexed by the metatable for this type. This ubox then contains the
mappings from pointer to full userdata. 
To avoid having to know the type from C++ side when destroying an object
I would create a new mapping table "pointer(lightuserdata) to ubox" so I
can find the userdata and additionally clear the entry in the ubox for
the object being destroyed. Additionally I need to change the places
where tolua++ functions access the userdata and add checks whether it is
marked invalid.

Would this be the way to go or am I missing something, is there a better
way? How are you invalidating your userdata when working with tolua++
and not giving ownership completely to Lua?

Regards,
Michael

-- 

Raritan Deutschland GmbH, Scheringerstr. 1, 08056 Zwickau
phone +49 375-271349-0 fax +49 375-271349-99
Geschäftsführer: Gerard W.J. Heuvel
USt-IdNr. DE813094160, HRB 18082 Chemnitz