The idea is that finalizers are not object methods as in Java. Rather, you can
register a finalizer function with any object. The finalizer takes no
arguments. The finalizer function is not allowed to hold a reference to the
object it is registered with - if it does, then the object will never be freed.
This solves the problem of finalizers making an object live during the process
of destruction.
A typical design pattern for finalizers will involve two objects: An outer
shell which contains the API to talk to some resource, and an inner object
which is the resource holder. An example would be a reader or writer interface
for the outer shell, and a file handle for the inner resource.
The finalizer function is registered on the outer shell, and holds a reference
to the inner object. (The finalizer may be a bound method on the inner object.)
When the outer shell is freed, the finalizer is run, which will release the
resource. Since the finalizer never holds a reference to the outer shell, it
can't prevent it from being freed and it can't change a dead object to a live
one during destruction.
Original issue reported on code.google.com by viri...@gmail.com on 1 Mar 2011 at 11:00
Original issue reported on code.google.com by
viri...@gmail.com
on 1 Mar 2011 at 11:00