ADC Home > Reference Library > Technical Q&As > Graphics & Imaging > Carbon >

Not Recommended Documentclose button

Important: The information in this document is Not Recommended and should not be used for new development.

Current information on this Reference Library topic can be found here:

Providing QuickDraw with a Known Good Port

Q: How do I provide QuickDraw with a known good port?

A: In many cases, you need to dispose of the object (GWorldPtr, 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]

Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.