Path: utzoo!utgpu!watserv1!watmath!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!uniwa!fennel.cc.uwa.oz.au!a_dent From: a_dent@fennel.cc.uwa.oz.au Newsgroups: comp.sys.mac.programmer Subject: Another THINK Pascal gotcha - "With" instance vars Message-ID: <1991Jan25.233122.2825@fennel.cc.uwa.oz.au> Date: 25 Jan 91 15:31:22 GMT Organization: University of Western Australia Lines: 33 I found another way to crash Think Object Pascal applications!!! In the manual, they warn against passing instance variables by reference, as they may move by the time the called routine returns. This is a very easy trap to fall into, despite the warning. They DIDN'T extend the warning to cover the "with" statement. As every keen Mac Pascal programmer knows, if you dereference a handle using "with handleName^^ do begin ...." you must lock the handle beforehand. The same applies to using "with" to dereference record structures of instance variables. Structured instance variables are still part of a handle (the object itself) and so must be locked before the "with", using a HLock(handle(SELF)). This is a far subtler bug than passing instance variables by reference as the window of vulnerability is just whilst executing the code inside the "with" and may be quite small (depending on what you call). However, even if you don't call things that move memory, the presence of networking drivers such as Tops can cause memory moves inside ANY sequence of code. We only found this one because the client started noticing crashes when they added an LC and TOPS. I suspect that the THINK environment does a lot of locking of objects behind the scenes so this one probably also qualifies as a "only crashes compiled applications" scenario. 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)