lua-users home
lua-l archive

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


> String assignment is by reference, not value. So internally, what
happens (the 
> simplified version) is that a new string with the value "string" is
stuck 
> into the virtual machine. Then s is set to be a reference to that
"string" 
> (it does not copy the actual letters). The constant "string"
immediately goes 
> out of scope, but since s still refers to the collection of letters
"string", 
> that collection is not garbage-collected. The only way to garbage
collect it 
> is for s to go out of scope or to set s to something else (nil, 3,
and "asdf" 
> will all work), at which point everything has forgotten about
"string" and it 
> (eventually) gets garbage collected.

Yes but what happens if "string" isn't actually assigned to the global
s.That is, s is a global but of nil value.So each time s gets assigned
to, the __newindex metamethod gets called.This metamethod calls an
internal C routine wich assigns "string" (the pointer to it that is)
to an internal C var.The point of all of this is to allow exported C
vars which can be assigned to from lua as if they were norma lua
vars.But the string ("string").Doesn't actually get assinged to any
lua vars although the statement looks like it.s still has a nil value
so that the next time it is assigned to the metamethod and internal C
routine is called again to do the actual job.Will the string still not
be garbage collected?