lua-users home
lua-l archive

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

Thank you for the answers (that from Mark was helpful too). I wasn't expecting anything magic from Lua, the problem is clearly not solvable and need some care from the programmers. Anyway knowing the exact behaviour will help me convince my friend Lua has nothing less than other languages on that point (he likes facts, and your mail will surely give more credit to the explanation).


-----Message d'origine-----
De : [] De la part de Roberto Ierusalimschy
Envoyé : 6 juillet 2006 13:57
À : Lua list
Objet : Re: Adding a reference in a finalizer

> 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