[Prev][Next][Index][Thread]
Re: Destructors
On Wed, 15 Mar 2000, Rob Myers wrote:
> This is a shamefully basic question, but....
> I know that for constructors in Dylan I can specialize initialize() or
> make(), but what can I do for destructors?
Weeeell, Dylan doesn't have destructors quite the same way as C++ does
(neither does Java, IIRC). Garbage collection means that you don't know
when your objects will "really" go away and you can't "really" destroy
them manually.
Having said that, Functional Developer (and of course Harlequin Dylan ;-)
has a "finalize" generic function which fulfils a similar role and some
other fucntions to control when/how finalize gets called. It basically
gets called between all references to an object being removed and the
memory actually being reclaimed; you can also call it manually. I don't
know if Gwydion's d2c and Mindy provide the same.
Some gotchas (discussed in the FD/HD documentation):
1) The finalize method might be called more than once, so it should be
safe to do so (e.g., if you're interfacing to C, make sure not to free
something multiple times).
2) A finalize method with a singleton specialiser is useless, because the
method itself refers to the object, so the object will never be GCd and
finalize will never be called on it (unless you do so manually, I
suppose).
3) The finalize method might not be called at all; e.g., at program
termination, I expect all memory will be freed /en masse/ with as little
GC effort as possible. Therefore don't depend on *automatic* finalization
to, say, free vital Win32 system resources; it's more for helping to keep
resource usage down with less effort.
HTH,
Hugh
Follow-Ups:
References: