[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: caching pointers to Lua strings?
- From: Reuben Thomas <rrt@...>
- Date: Fri, 10 Jun 2011 09:42:05 +0100
On 10 June 2011 06:46, Florian Weimer <firstname.lastname@example.org> wrote:
> * Josh Haberman:
>> What if I guarantee that the string is still reachable,
>> even after it is removed from the stack?
> Tons of extensions already assume that this work,
> so it's unlikely that major Lua implementations break this assumption.
You can get away with it for a short time under normal circumstances,
i.e. when there's no GC before you use the string, but it will bite
you in the end!
The string has to be somewhere in "Lua space" or it will be gc'ed: you
can't expect the garbage collector magically to know you have a
pointer to a string stashed somewhere. If it's not referenced by the
stack or a Lua variable, possibly a private one like the registry, it
can and will be gc'ed. lua_tolstring calls luaV_tostring, which calls
luaS_new, which is a macro for luaS_newlstr, i.e. it's a normal Lua
string, subject to normal rules.
Of course, with Lua strings you also have another get-out: since only
one copy is stored per string, if you have a short one you may be
lucky and it be non-unique.