[gclist] Destructor FAQ
Robert A Duff
bobduff@world.std.com
Fri, 15 Mar 1996 14:21:12 -0500
> One thing I haven't seen (perhaps I haven't been paying attention
> -- it's been a busy week) is some mention of the OTHER mechanisms
> in these languages (Java, Modula-3, Lisp) for "finalization". You
> don't have to do it all with objects -- if the lifetime of the resource
> corresponds with the lifetime of some lexical scope, you can say
>
> TRY
> ...
> FINALLY
> ...
Yes, this can be useful sometimes. But the object-based kind is pretty
important to have, because you normally want the module that defines the
data type to define whether or not finalization is necessary, and what
it should do. If the only feature you have is the above one, then all
the clients have to worry about it. Ada 83 had exception handlers like
this, but we added finalization in Ada 95 for just this reason.
> and get guaranteed reclamation of that resource. Note that Modula-3
> and Java have threads as well -- there can be multiple threads of
> control, with multiple stacks of things-to-be-finalized, simultaneously
> in existence. If it gunks up garbage collection to much to get
> absolutely-positively-guaranteed-and-prompt finalization, then maybe
> that sort of finalization should be dealt with in some other way.
Well, don't throw the baby out with the bathwater. It's only the
complicated cases that cause trouble. The simple case is where I
declare a local stack-based variable that opens a file on initialization
and closes it on finalization. This case doesn't cause any trouble for
garbage collection, and it's the most important case to have. (It does
require that the language support stack-based variables, however, which
I think is desirable anyway.)
- Bob