[plt-scheme] scheme_add_managed with FFI

From: Chongkai Zhu (czhu at cs.utah.edu)
Date: Sat Oct 24 10:55:53 EDT 2009

Chognkai Zhu wrote:
> Many thanks. With some experiment, it seems the following works:
>
> #lang scheme
>
> (require scheme/foreign)
> (unsafe!)
>
> (define add-managed
>  (get-ffi-obj "scheme_add_managed" #f
>               (_fun (_pointer = #f) _scheme (_fun #:keep (box null) 
> _scheme _pointer -> _void)
>                     _pointer _bool -> _pointer)))
>
> (define (make-p)
>  (let ((p (malloc _pointer 'raw)))
>    (add-managed p (lambda (p _) (free p)) #f #f)
>    p))
>
> (define p1 (make-p))
> (define p2 (make-p))
>
> (ptr-ref p1 _int)
> (ptr-ref p2 _int)
>
> (collect-garbage)
>
> But I didn't store the (lambda (p _) (free p)) or the box anywhere. Is 
> it really needed?
>

Never mind. I was reading the doc without thinking: 'make-p is holding 
the box from being collected.

> Chongkai
>



Posted on the users mailing list.