[racket] Calling collect-garbage does not work during a FFI call?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Jun 25 10:14:27 EDT 2010

Garbage collection is not disabled during an FFI call. In fact, it's
difficult or impossible to disable GC at all.

Finalizers registered with `register-finalizer', however, run in a
separate Racket thread. So there's no particular guarantee that
finalizers will run before a `collect-garbage' call returns. You might
find that inserting a `sleep' call right after `collect-garbage' causes
some of your finalizers to run. That doesn't seem like a great
solution, though, and it may create other trouble if multiple Racket
threads try to use BDD functions or kill off a thread using a BDD

Another possibility is to use the lower-level functions provided in the
C API, such as scheme_add_finalizer(). (If you register a
Racket-implemented function that way, be sure to make it atomic, so
that Racket-level threads do no swap during the call to the finalizer.)

At Sun, 20 Jun 2010 09:41:38 -0400, Nick wrote:
> Hi,
> I'm using MzScheme v4.2.4, and I'm trying to use the CUDD Binary
> Decision Diagram (BDD) library, which is implemented in C. CUDD
> manages all the BDDs with reference counting and its own garbage
> collector. In my scheme code I would like to have all the reference
> counting automated. What I did is have the functions create new BDDs
> to automatically increment the reference counts, and then use
> register-finalizer to decrement the reference count. I understand how
> to do all this, and it works.
> The problem is that I need to sync up the CUDD garbage collector and
> the Scheme garbage collector. I want the Scheme garbage collector to
> be called right before the CUDD garbage collector is called, so that
> when BDD nodes are unreachable by Scheme, their reference counts go to
> 0, and CUDD can collect them. CUDD provides a hook to have a function
> called right before garbage collection occurs, so I thought this
> should be simple. The problem is that the collect-garbage calls in the
> hook appear to do nothing. None of the finalizers are called when I
> rely on the hook, but if I explicitly call collect-garbage, then the
> finalizers are called.
> I know the finalizers are called because I print a message in them,
> and I know the hook function is being called, because I print out a
> message in the hook function. I cannot just call collect-garbage
> myself, because the CUDD garbage collection is called by the package
> itself at unknown times. Operations internal to the package can cause
> a garbage collection at any time. I have tried to call
> scheme_collect_garbage from C, in a hook function implemented in C.
> And, I have implemented the hook in scheme, and had passed the
> function to the C procedure that registers the hook. For both methods,
> the hook is called, and collect-garbage doesn't work.
> Is garbage collection disabled or something during a FFI call? Is
> there another solution?
> Thanks,
> Nick
> _________________________________________________
>   For list-related administrative tasks:
>   http://lists.racket-lang.org/listinfo/users

Posted on the users mailing list.