lua-users home
lua-l archive

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


Hi Francesco,

> I've got a dirty problem using weak tables, I hope someone can help
> me. What I'm doing is to keep a table that map windows objects to plot
> objects using the windows as weak keys. In some cases I'm doing a
> reverse lookup on this table to find out which window is referencing a
> particular plot.
> 
> What troubles me is that in same cases the table still contain among
> the key some windows that are already finalized. I was thinking that,
> because the keys are weaks, they are eliminated from the table when
> the GC finalizes the object. The problem is that it does not seem to
> work that way and sometimes I can access an object already finalized.
<snip>

If I understand correctly your problem is not that the entries in your table
are not collected when they're finalized, but that you're accessing already
finalized windows. One workaround is to include a flag to your window object
indicating if it has been finalized or not:

for window, plot in pairs(weaktable) do
  if window.isdone() then
    -- do something
  end
end

If you really want to keep a "tight" correspondence between finalization and
collection, use a regular table and free resources as needed:

function finalizewindow (w)
  windowplottable[w] = nil
  w:free()
end

Cheers,
K

-- 
Computers are useless. They can only give you answers.
                -- Pablo Picasso

-- 
Luis Carvalho (Kozure)
lua -e 'print((("lexcarvalho@NO.gmail.SPAM.com"):gsub("(%u+%.)","")))'