9 Protecting Graphics Operations

To guarantee the proper closing of viewports in cases of errors, especially when a program manages several viewports simultaneously, a programmer should use dynamic-wind:

(let ([w (open-viewport "hello" 100 100)])
  (dynamic-wind
    ; what we want to happen first: nothing
    void
    ; the main program (errors constrained to this piece)
    (lambda () (draw-pixel 13))  ; an error
    ; what we would like to happen, whether the main program
    ; finishes normally or not
    (lambda () (close-viewport w))))