[plt-scheme] scheme_add_managed with FFI

From: Chongkai Zhu (czhu at cs.utah.edu)
Date: Fri Oct 9 21:39:54 EDT 2009

Hi all,

In one of my programs, I need to use custodian to manage some resource. 
So I wrote the following:

(define add-managed
  (get-ffi-obj "scheme_add_managed" #f
               (_fun (_pointer = #f) _scheme (_fun _scheme _pointer -> 
_void) _pointer _bool -> _pointer)))


(add-managed p (lambda (p _) (my-close p)) #f #f)

A side note: here p itself is a _pointer (print as #<cpointer>) returned 
from some other FFI call.

If I run the program for long enough, adding objects under the manage of 
custodians, and closing custodians once a while, with other process 
(other than PLT) actively using memory, eventually a segfault will 
happen when I'm shutdown a custodian (which has some p under it's 
management). Testing shows the segfault is related to my `add-managed', 
as commenting it the segfault also disappears.

I noticed in 
http://schemecookbook.org/Cookbook/PLTCustodianRegistration, the FFI 
type of scheme_add_managed is different from how I use it. Did I do 
something wrong using scheme_add_managed this way? Why does the segfault 
happen? Could anyone help elaborate? Thanks in advance.


Posted on the users mailing list.