Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!keith From: keith@Apple.COM (Keith Rollin) Newsgroups: comp.sys.mac.programmer Subject: Re: Another THINK Pascal gotcha - "With" instance vars Message-ID: <48579@apple.Apple.COM> Date: 28 Jan 91 22:51:12 GMT References: <1991Jan25.233122.2825@fennel.cc.uwa.oz.au> Organization: Apple Computer Inc., Cupertino, CA Lines: 55 In article <1991Jan25.233122.2825@fennel.cc.uwa.oz.au> a_dent@fennel.cc.uwa.oz.au writes: > >As every keen Mac Pascal programmer knows, if you dereference a handle using >"with handleName^^ do begin ...." you must lock the handle beforehand. You probably know this, but your general statement above isn't quite right. You only need to lock your handle if something in the body of the statement moves memory. For instance, a statement like the following is OK: WITH myRectHandle^^ DO BEGIN top := 0; left := 0; bottom := 100; right := 200; END; However, the following is bad: WITH myGDHandle^^ DO BEGIN gdPMap := NewPixMap; .... END; This is because the WITH statemnt will generate a pointer to the GDevice stored in the myGDHandle. However, NewPixMap will allocate memory, which could move the GDevice, invalidating the pointer Pascal has generated to it. >... >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. This isn't quite true, either. Drivers can only move memory at well defined times (like at accRun time, which is done when you call SystemTask). Drivers cannot move memory between two arbitrary statements in your application, because that would mean that they'd be moving memory at interrupt time. In other words, something like the following is OK: myPtr := myHandle^; myValue := myPtr^; You don't have to worry about anything - including drivers - moving memory in between those two statements. -- ------------------------------------------------------------------------------ Keith Rollin --- Apple Computer, Inc. --- Developer Technical Support INTERNET: keith@apple.com UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith "Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions