<h1>Tech Notes</h1><h2>Printing Causes a Crash</h2><p>When you use <span class="pre">CLOSE LPRINT</span> or <span class="pre">CLEAR LPRINT</span>, the grafport that was in use (the printer) is tossed away. If your code expects a valid grafport, it's in deep trouble. Consider this:</p>
<pre>
/* Bad example */
ROUTE _toPrinter
FOR x = 1 TO maxPages
FN print1Page(x)
LONG IF x = maxPages
CLOSE LPRINT ' May cause a crash
XELSE
CLEAR LPRINT ' May cause a crash
END IF
NEXT
ROUTE _toScreen
</pre>
<p>When the <span class="pre">CLEAR</span> or <span class="pre">CLOSE LPRINT</span> is encountered the drawing surface where we were scribbling goes away. Subsequent scribbles against that surface will fall through to some other indeterminate place. In System 9, you will almost certainly crash. If you dont crash in System 8, youll at least get a headache. System 7 usually (but not always) survived.</p>
<p>The cure is a simple one. When you finish printing a page, route the output back to the screen before you eject it. The example below relocates the <span class="pre">ROUTE</span> commands and works in all versions of the system software.</p>