The foreign function interface allows us to register objects with a finalizer that calls when an object is about to be GC-ed. However, there are certain resources that we'd like to reclaim in a controlled way. For example, if the object lives to the end of the Scheme session, no GC will ever happen on it. PLT Scheme provides custodians to do this resource management. This module includes a nice register-custodian primitive that's similar to register-finalizer. Please also note that an object can have both a finalizer and a custodian (must be weakly held by the custodian in this case to allow the finalizer to run), so you can have fine control on it.
The code here is designed to hold any number of objects with the same type. The undefined `MyClose' is the Scheme function that should do the actual job to close the object. The C callback is only generated once for all of the objects. If you only have one object to register, things can be simplified. `scheme_gc_ptr_ok' and `scheme_dont_gc_ptr' are needed to keep the C callback from being GCed before all the registered objects are shut down. If you also use a finalizer on the object, or if one object is manually closed before the custodian shuts it down, remember to call `remove-managed' to remove the object from the custodian.