Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!sunybcs!sbcs!vallon From: vallon@sbcs.sunysb.edu (Justin Vallon) Newsgroups: comp.sys.mac.programmer Subject: Re: HLock and Think C 4.0 Message-ID: <3997@sbcs.sunysb.edu> Date: 20 Nov 89 18:29:26 GMT References: <18.25645e4f@bio.embnet.se> Sender: news@sbcs.sunysb.edu Reply-To: vallon@sblw.UUCP (Justin Vallon) Organization: State University of New York at Stony Brook Lines: 40 In article <18.25645e4f@bio.embnet.se> Mats.Sundvall@bio.embnet.se writes: >Just got bit by not locking a handle! >[Creates an object in TC4.0, which uses handles, that contains a string] >[Fails first time, works second] >[First time, the segment is loaded and the handle is moved, second not] > >Then I locked through the object library to see if this is the method that >Think uses. I was surprised when I did not find that many HLock in their >code. One difference when they copy strings is that they copy pstrings >with CopyPString (that does a Blockmove). Is it safe to do a Blockmove >without locking the handle? I would expect that many more HLocks should be in >the code. Yes, it is safe to do a BlockMove without locking the handle. One of IM's appendicies is a list of routines that move/purge memory. BlockMove isn't in it, so you can use it without locking. However, you seem to have discovered the problem. The first call to your ANSI library causes a LoadSeg (see Segment Loader for details), since the library (is presumably) in an unloaded segment. Notice that LoadSeg is in the list of routines that may move/purge memory. Memory is moved, and the address to your string is now pointing into never-never land. The second call succeeds because the segment has been loaded, and LoadSeg is not called. Solution: (1) Make sure that all "safe" routines (ie: ones that do not move/purge memory) are always loaded. This insures that a disasterous LoadSeg is not called when you are vulnerable. (2) Be paranoid, and Lock everything all the time. This sort of defeats the purpose, and it's sort of like stopping at a green light, just in case. (1) is probably a better solution. I usually leave the libraries in the main segment, so that stuff like this doesn't happen. > Mats Sundvall > University of Uppsala > Sweden -Justin vallon@sbcs.sunysb.edu