From: Joe Strout
Subject: catching the destruction of an object?
Date: 
Message-ID: <joe-180319991014105768@chinacat.salk.edu>
Is there some way to define a class method to be called when an object
is garbage-collected?  Something like the __del__ method in Python?

I have a class which wraps an underlying (opaque) data structure
allocated by a shared library.  When my Lisp object dies, I need to be
sure to deallocate the underlying data, or I'll have a big memory leak. 
I've searched all the Lisp books on my shelf, but while they go on at
great length about creation and initialization of objects, none of them
mention destruction and deallocation.

Note that defining some arbitrarily-named method myself doesn't help; I
assume that there may be multiple references to an object, and I want
the garbage collection to happen automatically when the last reference
is unbound.  So I need to find the method (if there is one) which is
called by the system (CLOS, I presume) automatically.

Thanks,
-- Joe

-- 
,------------------------------------------------------------------.
|    Joseph J. Strout           Biocomputing -- The Salk Institute |
|    ···@strout.net             http://www.strout.net              |
`------------------------------------------------------------------'
From: Barry Margolin
Subject: Re: catching the destruction of an object?
Date: 
Message-ID: <%RcI2.329$p4.116294@burlma1-snr2>
In article <······················@chinacat.salk.edu>,
Joe Strout  <···@strout.net> wrote:
>Is there some way to define a class method to be called when an object
>is garbage-collected?  Something like the __del__ method in Python?

Some Lisp implementations provide "finalization".  It's not generally
implemented at a class level, but at the object level -- you add specific
objects and functions to a list.  When the object is about to be
garbage-collected, the function is called on the object.

>I have a class which wraps an underlying (opaque) data structure
>allocated by a shared library.  When my Lisp object dies, I need to be
>sure to deallocate the underlying data, or I'll have a big memory leak. 

That's precisely what finalization is for.

>I've searched all the Lisp books on my shelf, but while they go on at
>great length about creation and initialization of objects, none of them
>mention destruction and deallocation.

Check the documentation of your Lisp implementation, searching for
"finalization".

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.