Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!mips!apple!shebanow From: shebanow@Apple.COM (Andrew Shebanow) Newsgroups: comp.sys.mac.programmer Subject: Re: HLock Message-ID: <5888@internal.Apple.COM> Date: 20 Dec 89 01:56:50 GMT References: <1989Dec19.091303.13316@agate.berkeley.edu> <3373@hub.UUCP> Organization: Apple Computer Inc, Cupertino, CA Lines: 45 In article <3373@hub.UUCP> 6600pete@hub.UUCP writes: >From article <1989Dec19.091303.13316@agate.berkeley.edu>, by lippin@skippy.berkeley.edu (The Apathist): >>>DTS' official position now is that there are so many patched traps running >>>around calling the Memory Manager that you need to lock and unlock handles >>>around ANY trap call. >Additional input from DTS would be helpful, I'm sure. >------------------------------------------------------------------------------- >Pete Gontier | InterNet: 6600pete@ucsbuxa.ucsb.edu, BitNet: 6600pete@ucsbuxa >Editor, Macker | Online Macintosh Programming Journal; mail for subscription >Hire this kid | Mac, DOS, C, Pascal, asm, excellent communication skills Your wish is my command... Anyhow, this statement of DTS's position is a bit of an oversimplification. It is true that the lists of traps that may move or purge memory are out of date, and that traps which currently are considered "safe" may become "unsafe" sometime in the future. Our recommendation is that developers err on the side of caution: if you are passing a dereferenced field of a handle by address, either use a temporary or lock the handle. I do feel that there are some calls which are "safe" to use dereferenced handles with, like BlockMove, some string manipulation calls, etc. Use your better judgment (or "Toolbox Karma", if you will). Tim Maroney is absolutely correct about the efficiency of temp variables vs HLock/HGetState calls: temps are much, much faster. Also, people who are doing code like this: HLock(h); p = *h; p->x = 1; p->y = 2; HUnlock(h); are wasting cycles. The HLock/Unlock here is totally unnecessary, since the compiler isn't going to generate code which will move or purge memory. For more information about unsafe references, I would encourage everyone to read Scott Knasters most excellent book, "How to Write Macintosh Software." Have fun, Andrew Shebanow MacDTS