lua-users home
lua-l archive

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


Roberto:

On Thu, May 30, 2019 at 5:57 PM Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>
> > > I believe that many programmers stumble upon this while learning Lua.
> > Well, it is a very strange thing to hit, you need to do some weird
> > things, starting by clearing the "local" which holds it inside the
> > function. I just got to it because I was being defensive.
> It is weirded than that. You also need some alternative way to access
> the value that is being collected.

My example ( lua, not  the original C question ) did not access the
vars, but had observable side effects (prints + setting a global var
), that's how I know parameters can be collected. The example did an
unusual but not that weird thing, setting the parameter to nil ( I
sometimes do this in real code, there are cases where logic is clearer
when treating parameters as modifiable locas ) and a
collectgarbage('collect'), to simulate memory pressure.

This thing would normally be harmless in pure lua. My C code did a
stupid thing ( pcalling and keeping pointer without keeping the object
alive ), which I solved by doing the ( imho ) right thing, "If the
manual does not say it is not collected, assume it can be".

The rest was just trying to demonstrate it can happen.

> ... (In your case, you didn't have> access to the value itself, but to some C structure being closed by
> __gc.

I admit it again. I did it a stupid thing, I corrected it, I do not do
that now, but I wanted to know if the parameter lifetimes were somehow
extended to the return ( now I've proven they are not , so my
intelectual itch is fully satisfied )

> Another option, as already pointed out, is through a weak table.)
> That doesn't seem common for someone learning Lua.

It has __gc, observable finalizers, not that simple, but I'm still a
beginner (in Lua, specially the api ) and hit this things. May be
because it allows me to solve some complex interaction problems, so I
tend to go to the dark corners.

Francisco Olarte.