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

Current GDevice Dependencies


Q: My application is exhibiting strange drawing behavior, sometimes drawing to the wrong window or not drawing at all. What could be causing this?

A: It is well known that many Mac OS APIs rely on the current GrafPort setting to work their magic. What is less well known is that many calls also gather information from the current GDevice as well and this may be the cause of some of your problems.

In particular, make sure the current GDevice is set to the main device before creating any window or port via calls such as CreateNewPort, NewDialog, etc. Watch out for calls to SetGWorld that could leave the current GDevice and GrafPort set to something other than the main device.

This all applies to both Mac OS 9 and Mac OS X, but Mac OS X adds another wrinkle to the problem. On Mac OS X, the current GDevice and GrafPort are stored per-thread so you'll have to be doubly careful.

Experience has shown that most QuickDraw-related crashes on Mac OS X are due to an invalid current port. For example, it's easy to overlook that the current port may still refer to a GWorld that has been deallocated and the proven habit of bracketing all SetPort calls by saving and restoring the current port can have the consequence of perpetuating the invalid port for much longer than you would expect.


[Feb 13 2002]


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.