lua-users home
lua-l archive

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


Har vi någon plan B?

mvh
Joakim Hansson

> X-Authentication-Warning: localhost.localdomain: diego owned process doing -bs
> Date: Thu, 20 Dec 2001 01:00:50 -0200 (BRST)
> From: Diego Fernandes Nehab <diego@tecgraf.puc-rio.br>
> X-X-Sender: <diego@localhost.localdomain>
> X-Original-To: Multiple recipients of list <lua-l@tecgraf.puc-rio.br>
> Subject: Re: doubt about gc
> MIME-Version: 1.0
> To: Multiple recipients of list <lua-l@tecgraf.puc-rio.br>
> 
> Hi,
> 
> > I would like to know if when I execute the code shown below, the garbage
> > collector can "clean" the lost data.
> 
> x = {}
> y = {}
> x[y] = 1 -- lost data
> y = nil
> 
> > I ask this because I am using this type of indexation to put into Private
> > table private attributes concerning an object, and when this object is
> > destructed I want that your private data also be destructed.
> 
> With  normal tables,  keys are  considered by  the garbage  collector as
> references to their values. Therefore, it doesn't matter whether you set
> 'y' to  nil. The value  will stil  be referenced by  the key and  the GC
> won't be able to collect either the key or associated data.
> 
> Lua 4.1  introduced weak keys. When  traversing a table with  weak keys,
> the garbage  collector ignores  its keys  when checking  for references.
> Therefore, since you set  y to nil, there will be  no references to that
> old value and it will be collected. Once it is collected, the associated
> data can also be collected.
> 
> So, yes, you can do that with Lua  4.1. I had never seen any utility for
> this,  and I  am  glad  you showed  me  one. Hope  it  works  the way  I
> described. :-)
> 
> Regards,
> Diego.
> 

/Frazze, Alias Joakim Hansson