lua-users home
lua-l archive

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


> So the problem is that once my finalizer is called it's never called
> again, but I still have references to the object. Is that normal ?
> Since after the finalizer call I have access to my object in the
> global scope, what prevents me from calling methods on it ? In fact
> I've tested it with a slightly more complex object, and methods get
> passed an invalid object. Methods that will misbehave if I don't
> detect myself if the object I'm getting has already been finalized or
> not.
>  
> I don't mind getting an answer like "storing references to an
> object in its finalizer is forbidden", provided it's official (like
> explicitly stated in the manual). But even in that cases it sounds a
> bit scary (fortunately it's not possible to reproduce the case with
> pure lua).

The behavior of Lua in this topic is similar to Java's. It is valid
to store references to an object which is being finalized, but the
finalizer is called only once. Of course, if you invalidate an object
(in its finalizer) and then call a method on it, the method will get an
invalid object. This is the usual behavior of any language :) (I mean,
it is not Lua that invalidates the object, it is your finalizer code
that does that. From Lua point of view, the object is alive and well.)

So, I would say that storing a reference to a finalized object, although
valid (in the sense that Lua will go into an consistent state, and will
behave as predicted and documented), is not recomended (in the sense
that your program will probably do something weird).

-- Roberto