[net.micro.mac] Menu Bars and C Follies

dws@tolerant.UUCP (Dave W. Smith) (10/11/85)

Here's a cute little (user) bug that burned up several hours of my time.
Hopefully, a bit of public humiliation may save others from the same fate.

Consider the following (in Consulair C, for what that matters):

	MenuHandle	menus[N_MENUS];

	menu[APPLE] = NewMenu(APPLE, CtoPstr("\024"));
	AppendMenu(menu[APPLE], CtoPstr("About Mummble...;(-"));
	AddResMenu(menu[APPLE], 'DRVR');
	InsertMenu(menu[APPLE], 0);
	... others ...
	DrawMenuBar();

Looks O.K., right?  Well, when the menu bar came up, the apple symbol was
hilighted.  Every OTHER time I selected "About Mummble..." it changed state.
Blink on, blink off.  The other menus were well behaved.  Oh my.  Was I doing
a HiliteMenu(0) in the right place?  Yes.  Was I managing to polute memory
from some other place in my application?  Commenting out 98% of it seemed to
indicate that I was not.  Oh dear.  Maxsbug time?  Gadzooks!

Here comes the "C follies" part.  In reworking my standard application
skeleton, I'd isolated most of a modules constant definitions into header
files, providing #define's for all sorts of things, like menu numbers.  Then
I collected all of the stray MenuHandle's that were lying about, and grouped
them together into an array, using the menu numbers as indices.  (Anybody
see what's coming?)  In C, arrays start at zero.  The handle for the Apple
menu is the first, so it gets a "#define APPLE 0", right?  Wrong.  In several
places, _Inside_Macintosh_ warns against using 0 as an ID for things.  The
menu manager is one of those places.  NewMenu() will accept 0 for a menu ID
without complaint, but life will get really wierd.  Try it sometime.

The morals:  Know thy _Inside_Macintosh_, and be wary of zeroes.
-- 
  David W. Smith                 {nsc,ucbvax}!tolerant!dws
  Tolerant Systems, Inc.
  408/946-5667                    [Standard Disclaimer]