[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: sharing strings between lua and C
- From: Ben Sunshine-Hill <bsunshin@...>
- Date: Sun, 19 Jan 2003 12:33:41 -0800
On Sunday 19 January 2003 12:22 pm, "jimmyp_gr wrote:
> And one more thing.How exactly does garbage collection work?Say I have
> the __newindex metamethod set to a function of mine and I call
> something like s="string" in lua.This might assign string to s if it
> is a lua var or might copy the char * internally if it is a c var.Does
> the garbage collector collect the "string" string if it did not get
> assigned to a lua var or does he consider the value used(by s whatever
> that may be) and therefore not to be collected until another
> assignment to s?In the second case my troubles might be over.
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.