[plt-scheme] ffi and register-finalizer

From: Michael Reynolds (mtr26 at cs.byu.edu)
Date: Fri May 20 10:45:02 EDT 2005

I am doing a project that relies on the ffi and c pointers. As I 
understood, when you define a c pointer you can register a finalizer for 
it that scheme will dynamically use to correctly delete and remove the 
pointer from memory. 

For me it does not do this, the destructors are not called.  The printf  
runs and the pointer can be used but the destructor is not called 
automatically. I was wondering if anyone has more experience with this. 
Is it supposed to garbage collect your c pointers? If not is there a way 
to make it do that? Or is there some other error(s) in what I am trying 
to do?

Thanks for any help you can offer,

Michael Reynolds


(define-syntax dbxml-ctype
  (syntax-rules ()
    [(_ _name destructor s->c)
    (begin
     (define-cpointer-type _name #f s->c
       (lambda (ptr)
         (if ptr
           (begin (register-finalizer ptr destructor ) (printf 
"Registering ptr\n") ptr)
           (error '_name "got a NULL pointer"))))
     (defdbxml* destructor : _name -> _void))]
    [(_ _name destructor) (dbxml-ctype _name destructor #f)]))



Posted on the users mailing list.