Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!hoptoad!tim From: tim@hoptoad.uucp (Tim Maroney) Newsgroups: comp.sys.mac.programmer Subject: Re: ATP/Init question Message-ID: <7868@hoptoad.uucp> Date: 6 Jul 89 05:24:43 GMT References: <5916@hubcap.clemson.edu> Reply-To: tim@hoptoad.UUCP (Tim Maroney) Organization: Eclectic Software, San Francisco Lines: 64 In article <5916@hubcap.clemson.edu> mikeoro@hubcap.clemson.edu (Michael K O'Rourke) writes: >However, my machine bombs a few seconds after the init is loaded and i can't >figure out why. My guess is that i am trying to have global data in a way >which isn't kosher. I don't know what the correct or standard way is. > >Receive() >{ >OSErr err; > > RememberA0(); Here's the problem -- see below. > SetUpA4(); > if (RecBuff == 666) > SysError(1); > else if (RecBuff == 999) > ShutDwnPower(); > NewPBPtr->atpSocket = Socket; > NewPBPtr->reqLength = sizeof(RecBuff); > NewPBPtr->reqPointer = (Ptr) &RecBuff; > NewPBPtr->ioCompletion = &Receive; > err = PGetRequest(NewPBPtr,1); > RestoreA4(); >} Your problem is straightforward. RememberA0() depends on A0 being set up by LSC glue code to point to the start of the code resource. When Receive is called, it is called directly from the Macintosh OS, since it is a completion routine. A0 points to your parameter block, not the code resource, because that's how completion routines are called. Your globals operations go off into outer space and crash the system. The proper way to do this is to delete the RememberA0() call in Receive. The code that stashes the globals register in your main() is correct and needs no changes: >main() >{ >MPPPBptr OldPBPtr; >EntityName Entity; >OSErr err; > > RememberA0(); > SetUpA4(); > [routine body deleted] > RestoreA4(); >} I also have a few qualms about reusing the parameter block from inside a completion routine. I'm not sure whether or not this is safe; I would use two parameter blocks and alternate them just to feel secure. If it's not safe, you risk crash or deadlock with a corrupted request queue. Can someone answer this issue authoritatively? Inside Mac doesn't seem to mention whether the block has already been dequeued when the completion routine is called. In the absence of documentation, it's not safe to depend on empirical evidence, since things could change with the next system release. -- Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com Postal: 424 Tehama, SF CA 94103; Phone: (415) 495-2934 "Skip, witches! Hop, toads! Take your pleasure!" -- Aleister Crowley, THE BOOK OF LIES