[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Problem with circular refrence and garbage collection
- From: <malobu@...>
- Date: Sat, 10 Jan 2009 18:40:48 -0800
Thanks for the suggestion, i will try that.
How I reference the function with the button(yes a userdata) is as follows:
In the registry there is a reference to a table(weak keys) that maps each widget to a table where all of the widgets references are stored. The function is referenced in this table via luaL_ref().
What I expected to happen is this.
1. frame is collected, and its refrence table is collected.
2. col is collected, and its refrence table is collected.
3. button is collected, and its reference table is collected.
4. the closure is collected
only step 1 is happening.
---- Sam Roberts <firstname.lastname@example.org> wrote:
> On Sat, Jan 10, 2009 at 12:05 AM, <email@example.com> wrote:
> > I am creating a Lua api for a gui toolkit i am working on and have run
> > into a snag with garbage collection not happening when there is a
> > circular refrence.
> > --After this chunk is run there is no refrence to frame, it is collected
> > eventually the problem is col and the button that is attached to it is
> > never collected
> > local frame = cwl:createFrame("Hello World")
> > --attachTo will create a refrence to col in frame, which goes away when
> > the frame is collected
> > local col = cwl:createColumn():attachTo(frame)
> > --attachTo will create a refrence to button in col
> > local button = cwl:createButton():attachTo(col)
> > --the function is refrenced by the button to lock it into existence
> > until the button is collected.
> > --but when I create this closure with col as an upvalue, it locks col
> > into existence, when frame is collected, col is not becuase of this
> > refrence.
> > button:onEvent("click", function(b) col:selectRow(i) end)
> How does button (a userdata, I assume?), keep the reference to the function,
> using luaL_ref()? I "think" that if it uses the userdata's fenv, that
> lua would see the cycle, and be able to collect.