[gambit-list] call on gc

Marc Feeley feeley at iro.umontreal.ca
Sun Jun 28 09:49:44 EDT 2009


On 27-Jun-09, at 10:27 PM, lowly coder wrote:

> Is it possible to have an object X so that some function gets called  
> when X is gc-ed?
>
> Suppose I have something like
>
> (define-type foo ...)
> (define bar (foo-create ...))
>
> and inside of bar, there's a C-land resource that I want to have  
> freed up when bar is GC-ed;
>
> Is there a way to have some function like (foo-gc) s.t. when bar is  
> GC-ed,
> (foo-gc bar) is called?
>
> Thanks!

There are (at least) two ways to achieve this:

1) wills:

    (define alloc-ptr (c-lambda () (pointer char) "alloc_ptr"))
    (define free-ptr  (c-lambda ((pointer char)) void "free_ptr"))
    (define-type foo ptr)
    (define bar (make-foo (alloc-ptr))))
    (make-will bar
               (lambda (obj) (free-ptr (foo-ptr obj))))
    ...

2) release function on "pointer" FFI type: see https://webmail.iro.umontreal.ca/pipermail/gambit-list/2009-May/003475.html 
  for an example.

Marc




More information about the Gambit-list mailing list