[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Light userdata
- From: David Jones <drj@...>
- Date: Thu, 26 Jun 2003 10:12:35 +0000
In message <bddmpo$jjf$1@main.gmane.org>, "Dylan Cuthbert" writes:
> Question,
>
> Light userdata is simply a pointer to some internal C/C++ (native) data, and
> that data could be inside another structure, right?
>
> Light userdata has no metatable.
>
> Ok, so how do I destroy all uses (or nil them at least) of that light
> userdata in lua when the C/C++ class/object its pointing to is destroyed?
Don't do that.
If you have an object (or pool of objects, eg instances of some class)
that must have an existence on both the lua and the C side then either
lua or C must be responsible for managing the objects lifetime.
If you can destroy the object from C then you can't let Lua have a
pointer to it in the form of a userdata.
There are a few approaches. You can insert a proxy object which
has a 1-to-1 correspondence to the real object, but reports "i am
dead" whenever you try to do anything with it (from the lua side)
after the real object has been destroyed. You still have to manage the
proxy objects though.
My preferred approach in such situtations is to use integers on the
Lua side and an array on the C side.
Note you also have the same problem in C anyway. When you destroy
an object in C how to ensure that all pointers to it on the C side
are NULLed out? Answer: Much error prone, hard, and fragile work.
The alternative is to let Lua manage the lifetime and destroy the object
on the C side using a gcmethod. You can't do this if the object is
"precious"; for example a frame buffer, file descriptor, shared mutex,
audio mixer channel.
Cheers,
drj