From: Karol Skocik
Subject: 'destructors' in Lisp?
Date: 
Message-ID: <1124827862.541888.116900@g44g2000cwa.googlegroups.com>
Hi,
  I have this problem :
my code interfaces to some C code using CFFI, and I use
foreign-object-alloc to allocate foreign structs. I have a function
like 'create-something' which returns Lisp structure (actually a
wrapper around C structure), and I want to return C memory back when
garbage collector collects the Lisp wrapper when it is not referenced
any more.
  Is there any way how to avoid manually calling ugly
'destroy-something' and let the GC call the destroyer function silently
when GC decides to collect the Lisp wrapper? I would like to stay away
from CLOS for now (since I am newbie), I am using defstruct for Lisp
wrapper.

Thanks for any suggestions.

Regards,
  Karol

From: M Jared Finder
Subject: Re: 'destructors' in Lisp?
Date: 
Message-ID: <G6KdnZ2dnZ3qHvbznZ2dnXhjlt6dnZ2dRVn-zJ2dnZ0@speakeasy.net>
Karol Skocik wrote:
> Hi,
>   I have this problem :
> my code interfaces to some C code using CFFI, and I use
> foreign-object-alloc to allocate foreign structs. I have a function
> like 'create-something' which returns Lisp structure (actually a
> wrapper around C structure), and I want to return C memory back when
> garbage collector collects the Lisp wrapper when it is not referenced
> any more.
>   Is there any way how to avoid manually calling ugly
> 'destroy-something' and let the GC call the destroyer function silently
> when GC decides to collect the Lisp wrapper? I would like to stay away
> from CLOS for now (since I am newbie), I am using defstruct for Lisp
> wrapper.

There are two solutions available to you.  If the object will be 
shortlived, like a file handle, you can make a macro like 
with-open-file.  This is completely portable Common Lisp code, but it 
only works for objects which could have dynamic extent (stack allocated).

If you want the object to have indefinite extent (heap allocated), but 
you don't want to be responsible for calling a cleanup function, you'll 
need to consult your Lisp for how to attach a finalizer to an object.

Usually, a with-* macro will be enough.

   -- MJF
From: Pascal Bourguignon
Subject: Re: 'destructors' in Lisp?
Date: 
Message-ID: <87wtmc4cyr.fsf@thalassa.informatimago.com>
"Karol Skocik" <············@gmail.com> writes:

> Hi,
>   I have this problem :
> my code interfaces to some C code using CFFI, and I use
> foreign-object-alloc to allocate foreign structs. I have a function
> like 'create-something' which returns Lisp structure (actually a
> wrapper around C structure), and I want to return C memory back when
> garbage collector collects the Lisp wrapper when it is not referenced
> any more.
>   Is there any way how to avoid manually calling ugly
> 'destroy-something' and let the GC call the destroyer function silently
> when GC decides to collect the Lisp wrapper? I would like to stay away
> from CLOS for now (since I am newbie), I am using defstruct for Lisp
> wrapper.

This is an implementation dependant question. You should check the
documentation of your implementation(s).

In clisp 2.34, you can use EXT:FINALIZE to install a finalization
handler on an object.

-- 
"Klingon function calls do not have "parameters" -- they have
"arguments" and they ALWAYS WIN THEM."
From: Karol Skocik
Subject: Re: 'destructors' in Lisp?
Date: 
Message-ID: <1124829875.706410.288090@g43g2000cwa.googlegroups.com>
Thats it, thanks! 

Karol
From: Marco Baringer
Subject: Re: 'destructors' in Lisp?
Date: 
Message-ID: <m2k6ibsubp.fsf@soma.local>
Pascal Bourguignon <····@mouse-potato.com> writes:

> In clisp 2.34, you can use EXT:FINALIZE to install a finalization
> handler on an object.

this was in foil's sourcecode (foil.sourceforge.net):

(defun finalize (object action)
  #+lispworks (declare (ignore action))
  #+lispworks (hcl:flag-special-free-action obj)
  #+cmu (ext:finalize ret action)
  #+sbcl (sb-ext:finalize ret action)
  #+allegro (excl:schedule-finalization ret action)
  #+openmcl (ccl:terminate-when-unreachable object action)
  object)

on lispworks you will also need to call:

(hcl:add-special-free-action 'NAME-OF-FUNCTION-PASSED-TO-FINALIZE)

hth.

-- 
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
	-Leonard Cohen
From: Pascal Costanza
Subject: Re: 'destructors' in Lisp?
Date: 
Message-ID: <3n1g5dF1836bgU1@individual.net>
Karol Skocik wrote:
> Hi,
>   I have this problem :
> my code interfaces to some C code using CFFI, and I use
> foreign-object-alloc to allocate foreign structs. I have a function
> like 'create-something' which returns Lisp structure (actually a
> wrapper around C structure), and I want to return C memory back when
> garbage collector collects the Lisp wrapper when it is not referenced
> any more.
>   Is there any way how to avoid manually calling ugly
> 'destroy-something' and let the GC call the destroyer function silently
> when GC decides to collect the Lisp wrapper? I would like to stay away
> from CLOS for now (since I am newbie), I am using defstruct for Lisp
> wrapper.
> 
> Thanks for any suggestions.

ANSI Common Lisp doesn't define such destructors. You have to revert to 
implementation-specific extensions. Check the manual of your CL 
implementation.

If you want to write portable code, either implement a common API as a 
wrapper for those extensions, or try to think of a way how to confine 
such objects within the control flow of a program. In the latter case, 
you could then use unwind-protect to automatically release your foreign 
objects.


Pascal

-- 
OOPSLA'05 tutorial on generic functions & the CLOS Metaobject Protocol
++++ see http://p-cos.net/oopsla05-tutorial.html for more details ++++