[comp.sys.mac.programmer] Confused Mac Programmer Seeks Help...

perry@key.COM (Perry The Cynic) (05/20/89)

Hm. I guess it's my turn to ask a stupid question. But I can't seem to figure
this out. Consider this rather simple C program:

main()
{
	WindowPtr w;	TEHandle te;	Rect bounds;
	... standard toolbox incantation here ...
	SetRect(&bounds, 50, 50, 300, 300);
	w = NewCWindow(0, &bounds, "\pTest Window", TRUE, 0, (WindowPtr)(-1), FALSE, NULL);
	SetPort(w);
	SetRect(&bounds, 2, 2, 230, 230);
	te = TEStylNew(&bounds, &bounds);
	TEActivate(te);
	TEKey('a', te);
}

Not that it does anything useful - it just creates a window with a new-style
(styled) TextEdit record in it, activates it and puts one character in.
Compile it and run it - it works. No problem.

Now use your favorite low-level debugger (e.g., TMON), and turn on
*heap scramble*. Run this program again. Ooomph. Various strange system
bombs, happening in TEKey. The bomb happens if and only if heap scramble
is active during execution of TEStylNew.

Closer examination shows that TEStylNew creates (as one of several dozen)
an eight-byte relocatable block. The block is never locked during execution of
TEStylNew, and eventually the trap code stores something into the
second longword of that block (the first one never gets set at all...).
It seems that TEStylNew keeps a pointer into that (unlocked!) block. Without
heap scramble, the value appears there eventually. With it, the block is
left uninitialized ('XYXY' with TMON).
TEKey tries to use the value in that block and chokes. Many other text traps,
BTW, use it too (TECut is one).

SO: What is going on here? Is Apple's toolbox code for TEStylNew violating
a Main Rule of Macintosh programming? Has THINK C a problem? Is there a
bug in TMON's heap scramble code? Or what am I overlooking here?

Vital Statistics: Macintosh II. System Software 6.0.2, Multifinder.
THINK C 3.0.2. TMON 2.8.1, using "Heap check, scramble" (purge doesn't
matter).

Can somebody help me? Even telling me that "it doesn't fail on my machine"
may be useful. If I'm overlooking something obvious, please bear with me.
Thanks in advance
  -- perry

P.S.:	The problem is NOT in the lack of an event loop in the test program
	above, nor in any particular values for the trap calls. It
	consistently happens in any program where I try to use styled TextEdit.
	Also, it persists when I boot a minimal system from floppy (TMON's
	INIT is still there, of course).
-- 
------------------------------------------------------------------------
Perry The Cynic (Peter Kiehtreiber)		     perry@arkon.key.com
** What good signature isn't taken yet? **	   ...!pacbell!key!perry