[Prev][Next][Index][Thread]
Re: Destructors
Rob Myers <robmyers@cwcom.net> writes:
> 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?
I wrote a brief note on this topic in Dylan about a week ago for my
site - there's probably nothing there you don't already know though as
it doesn't go into finalization details:
http://www.double.co.nz/dylan/tips/resourceacquisition.htm
As others have said, finalization is an option if you are using
FD. You do have to remember to call drain-finalization-queue
occasionally or have automatic-finalization-enabled? set to #t. You
must also explicitly call finalize-when-unreachable on your object if
you want the finalizer method called. There is no guarantee that the
finalizer will be called.
The safest way of ensuring construction/destruction type idioms would
be to use block/cleanup forms I think. Wrapped in a macro like the
with-open-file and with-lock type macros. Maybe add a resource release
in the finalizer as well in case the user of the class does not use
the with-* macro, or forgets to call the cleanup function. Harlequin
Dylan does this in the SQL ODBC library for cleanup up SQL resources
(statement handles, etc).
Incidentally, I asked almost this exact question in my very first
comp.lang.dylan post back in 1998 - finalization was the option
presented back then too.
Chris.
Follow-Ups:
References: