lua-users home
lua-l archive

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


> I need to remove a lua reference to a C++ pointer object. In wxLua you
> can create wxWindows widgets that will be destroyed by their parent
> window by wxWindows.
[.. snip snip ..]
> so I
> guess what I'm asking for is how do I do the reverse (remove the
> reference by the pointer's value).

This is a very common phenomenon.  The solution is to not store the pointers
directly into the Lua userdata.  Instead you keep an in-between
administration that maps "handles" --used at the lua side-- to the actual
c++ pointers and vice versa.  Handles could be just numbers (*).  Then
whenever lua invokes an operation on such a handle, you check it for
validity first on the c++ side.

If a pointer is invalidated from the c++ side simply invalidate its
corresponding entry in the handle administration (e.g. store some kind of
NULL value) thereby implicitly invalidating all handles in lua that are
still bound to it.

You can use lua tables (stored in the lua registry or some upvalues) for the
administration or you can setup your own, whichever is more convenient.  If
you implement the admin as lua tables you can simply store pointers by
casting them to int, because they will remain completely under your own
control.

If you need some more explicit hints let me know.

(*) But better not use (int)(pointer) as a handle, because memory can easily
be recycled...

--
Wim