[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Re: photo album in chicken
From: |
Graham Fawcett |
Subject: |
Re: [Chicken-users] Re: photo album in chicken |
Date: |
Mon, 1 May 2006 10:50:20 -0400 |
On 5/1/06, Peter Busser <address@hidden> wrote:
Is it possible to call a C function when data is destroyed during
garbage collection? In that case, one could create a Scheme type which
encapsulates the pointer to the Imlib data. When an object of that type
is destroyed, the hook can be called, which in turn frees the C data.
Sounds like you want (set-finalizer! X PROC) : "Registers a procedure
of one argument PROC, that will be called as soon as the non-immediate
data object X is about to be garbage-collected (with that object as
its argument). Note that the finalizer will not be called when
interrupts are disabled. This procedure returns X."
An example, hastily copied from the iconv egg:
(define iconv-open-inner
(foreign-lambda* c-pointer ((c-string tocode) (c-string fromcode))
"iconv_t result = iconv_open(tocode, fromcode);"
"if (result == (iconv_t) -1)"
" result = NULL;"
"return(result);"))
(define (iconv-open tocode fromcode)
(and-let* ((value (iconv-open-inner tocode fromcode)))
(set-finalizer! value (foreign-lambda void "iconv_close" c-pointer))
value))
Graham