The Scheme garbage collector destroys objects of type context or widget that are not longer accessible from within the Scheme program. This is done by invoking the function destroy-context or destroy-widget, respectively, with the unreferenced object as an argument.
The garbage collector only destroys objects that have been created from with the Scheme program (by functions like create-context or create-widget). Objects that have been obtained from Xt through functions like widget-context (and are owned by the Xt 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-context) are ignored or assigned to local variables as in
(define (initialize) (let* ((con (create-context)) (dpy (initialize-display con #f 'Test #f))) (create-shell 'Test #f (find-class 'application-shell) dpy))))
In this example, after termination of the function, the garbage collector will destroy the application context created by the call to create-context as well as the display, as they are no longer directly accessible from within the program. Bugs like this are often hard to find, since (in the above example) the shell widget returned by initialize can still be used, although its application context and the display associated with the application context have already been destroyed.
The problem can be solved by re-writing the above function like this:
(define initialize #f) (let ((con) (dpy)) (set! initialize (lambda () (set! con (create-context)) (set! dpy (initialize-display con #f 'Test #f)) (create-shell 'Test #f (find-class 'application-shell) dpy))))
An alternative solution is to place the application context and display into a global variable, so that they can be terminated explicitly by the program when desired.