How do I provide QuickDraw with a known good port?
In many cases, you need to dispose of the object (
CGrafPtr, etc.) that is the current port. However,
you should never leave QuickDraw with an invalid current
port so you must always set the current port to a known good
port before disposing of the object. The real question is,
what do you do if you don't have a known good port to set?
Before Carbon and Mac OS X, you could set the current port to the
Window Manager port (via
LMGetWMgrPort) in order to leave QuickDraw
in a valid state. However,
under Carbon, the Window Manager port is no longer available. In addition,
QuickDraw on Mac OS X is even pickier about having a valid port due to
the use of protected memory.
Thankfully, as of Mac OS X 10.1, QuickDraw provides a simple solution: call
SetPort( NULL ).
SetPort( NULL ) on Mac OS X 10.1 and later has
the useful semantic of setting the current port to a "fallback port" which will
prevent crashes due to bad dereferences but has an empty
bounds to prevent unwanted drawing. This is very similar to the behavior that the
Window Manager has had for years. Since Mac OS 8.5 (and still present in Mac OS X),
the Window Manager has automatically
set the port to a scratch port whenever you destroy the window containing the current port.
Please note that the
SetPort( NULL ) semantic is not supported on Mac OS 9,
even for Carbon applications.
[Sep 29, 2003]