chickadee » jiffi » call-with-gc-root

call-with-gc-root value procprocedure

Creates a GC root for the given Scheme value, and calls proc with the GC roots as arguments. After proc returns, deletes the GC root and returns the result of proc.

proc must be a procedure which accepts one argument, a pointer to the GC root which can be passed to gc-root-ref to get value. The GC root pointer will become invalid after proc returns.

If proc does not return normally (e.g. if an exception occurs, or if you invoke a continuation), the GC root will not be deleted, which will cause a memory leak. Use gc-root-delete! within proc to manually clean up the GC roots before raising an exception or calling a continuation.

Example:

(let ((a (list 1 2 3)))
  (call-with-gc-root a
    (lambda (a-root)
      (assert (eq? a (gc-root-ref a-root)))
      'ok!)))
;; ⇒ ok!

This is a convenience procedure based on make-gc-root and gc-root-delete!.