Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bionet!agate!ucbvax!dewey.soe.berkeley.edu!oster From: oster@dewey.soe.berkeley.edu (David Phillip Oster) Newsgroups: comp.sys.mac.programmer Subject: Re: Question on Handle locking example Message-ID: <27801@ucbvax.BERKELEY.EDU> Date: 29 Jan 89 01:33:09 GMT References: <379@lclark.UUCP> <2580011@hpausla.HP.COM> <1219@sw1e.UUCP> Sender: usenet@ucbvax.BERKELEY.EDU Reply-To: oster@dewey.soe.berkeley.edu.UUCP (David Phillip Oster) Organization: School of Education, UC-Berkeley Lines: 47 In article <1219@sw1e.UUCP> uucibg@sw1e.UUCP ([5-3929] Brian Gilstrap) writes: >I'm not clear on why function 'a' below would die. This was posted recently >as an example of how not to code. I'm sure I'm missing something extremely >obvious (it's late...), but please explain.... > >>a() >>{ >> HLock(h); >> b(h); >> *h->x = ... /* this would not survive a heap scramble */ >> HUnlock(h); >>} B was defined as: b(h)Handle h;{ HLock(h); ... HUnlock(h); } so the bad line in a gets executed with h already unlocked. One fix is to write: b(h)Handle h;{ char state; state = HGetState(h); HLock(h); ... HSetState(h); } Note: before you start coding with this style, disassemble the glue your system uses for HGetState(). If it directly calls the system trap, you are going to have to use: unsigned char MyHGetState(h)Handle h;{ if(world.machineType > envMac) return HGetState(h); else return *((unsigned char *) h); } and similarly for HSetState, as the code says, classic macs don't have this trap. (You'll have to do a SysEnvirons(1, &world); in your Init() routine.) } --- David Phillip Oster --"When we replace the mouse with a pen, Arpa: oster@dewey.soe.berkeley.edu --3 button mouse fans will need saxophone Uucp: {uwvax,decvax}!ucbvax!oster%dewey.soe.berkeley.edu --lessons." - Gasee