[comp.sys.mac.programmer] gotcha "with" vars - WHO MOVED THE MEMORY

a_dent@fennel.cc.uwa.oz.au (02/03/91)

In article <48579@apple.Apple.COM>, keith@Apple.COM (Keith Rollin) writes:
> In article <1991Jan25.233122.2825@fennel.cc.uwa.oz.au> a_dent@fennel.cc.uwa.oz.au writes:
>>
>>As every keen Mac Pascal programmer knows, if you dereference a handle using
>>"with handleName^^ do begin ...." you must lock the handle beforehand.
> 
> You probably know this, but your general statement above isn't quite
> right. You only need to lock your handle if something in the body of
> the statement moves memory. 
Yeah, I know and I'm too lazy to memorize the list of relevant traps - I'd
rather just lock handles in doubt, and forgot to apply that technique in
this case.

> This isn't quite true, either. Drivers can only move memory at well
> defined times (like at accRun time, which is done when you call
> SystemTask).  Drivers cannot move memory between two arbitrary
> statements in your application, because that would mean that they'd be
> moving memory at interrupt time. In other words, something like the
> following is OK:
> 
> 	myPtr := myHandle^;
> 	myValue := myPtr^;
> 
> You don't have to worry about anything - including drivers - moving
> memory in between those two statements.
> 
Ummm.
CRINGE CRINGE CRINGE CRINGE CRINGE CRINGE CRINGE CRINGE CRINGE CRINGE CRINGE 
I was calling DrawText within that "with" block - which has been documented
since the original 3-volume set of Inside Mac as one of the traps which may
move memory.

Like I said, I normally lock all vulnerable handles.

I guess this is an object lesson (pun intended) in just how lenient the Mac
can be with doubtful programming.

This DrawText call seems to be fairly rugged as it has survived tens of
thousands of these "vulnerable" calls without problems.  And, honestly, it
was only moving memory when we used the LC over TOPS :-)

On another note, I just found another bug using DrawText where one combination 
of ads (its a magazine printing program) caused a call to DrawText with a text
length of zero.  Again, DrawText seems to tolerate this 99.9% of the time, 
making the bug very hard to track down.

For those who are interested, I used the brute-force technique of replacing all 
calls to DrawText to a procedure of my own, which verified the parameters 
before passing them on.  Sometimes a few simple-minded approaches like this
can save hours of theorizing...

> -- 
> ------------------------------------------------------------------------------
> Keith Rollin  ---  Apple Computer, Inc.  ---  Developer Technical Support
> INTERNET: keith@apple.com
>     UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith
> "Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions

Andy Dent                     A.D. Software phone 09 249 2719
Mac & VAX programmer          94 Bermuda Dve, Ballajura
a_dent@fennel.cc.uwa.oz       Western Australia  6066     
a_dent@fennel.cc.uwa.oz.AU (international)