[gclist] RE: Destructor functions and GC

Amit J Patel amitp@Theory.Stanford.EDU
Thu, 7 Mar 1996 12:00:08 -0800


Thomas Weitzel wrote to us on March 7:
> 
> > Some of the revent posts on GC and OO have reminded me of a problem that 
> > occurred to me some years ago: how does a GC interact with destructor 
> > functions?

> I definitely agree that it is desirable to have the opportunity to
> have a finalization function called when an object is "freed" by the
> garbage collector. The typical situation when one wants to have this
> is when an object is associated with some "external" resource like a
> disk file (or a semaphore).  I've seen some advice that it is
> considered bad practice to rely on finalization, but I don't know
> how to deal with it in some other way without introducing the
> possibility of invalid references (like the attempt to read from a
> file which was just closed by some innocent looking piece of
> code). Any opinions on this?  

Perhaps finalizers on all live objects should be called when the
program exits, so that we _can_ rely on finalizers to be called at
some point.

I think finalizers are the right way to deal with these resources.
Essentially the finalizer has to be something that MUST be done at
SOME time, but there is no guarantee on WHEN it will be done.  If the
finalizer does something that depends on when it is done, then it is
the programmer's fault for making that assumption.

	- Amit


-- 
Amit J Patel, Computer Science Department, Stanford University