21.  Interaction with the Garbage Collector  

      The Scheme garbage collector destroys objects of type colormap, cursor, display, font, gcontext, pixmap, or window that are not longer accessible from within the Scheme program. This is done by invoking the function free-colormap, free-cursor, close-display, close-font, free-gcontext, free-pixmap, or destroy-window, respectively, with the object to be destroyed as an argument.

      The garbage collector only destroys objects that have been created from with the Scheme program (by functions like create-pixmap or open-display). Objects that have been obtained from the Xlib through functions like display-default-colormap (and are owned by the Xlib internals), are ignored by the garbage collector.

      Programmers must make sure that an object is accessible during the object's entire lifetime, otherwise future runs of the garbage collector can result in undesired termination of the object. One must be especially careful when results of functions that create new objects (such as create-window) are ignored or assigned to local variables as in

(define dpy (open-display))
(define root (display-root-window dpy))
(do ((x 0 (+ x 10)) (y 0 (+ y 10))) ((= x 50))
  (let ((win
          (create-window 'parent root 'x x 'y y 'width 20 'height 20)))
    (manage-window win)))

      In this example, after termination of the do-loop, the garbage collector will destroy the newly created windows, as they are not accessible from within the program. If this is not desired, the windows could be put into a variable (for instance, be consed into a list) that is defined outside of the body of the loop.

Markup created by unroff 1.0,    September 24, 1996,    net@informatik.uni-bremen.de