[comp.sys.mac.programmer] When to HLock HandleObjects

bowman@reed.UUCP (Eric Bowman) (01/14/91)

Has someone, somewhere, sometime elucidated when to lock down a handle
object? (i.e. calling constructors, virtual functions, etc.)

Currently I'm proceeding somewhat by trial and error (with success...apparently)
but it seems like a sticky point.

Any references appreciated (d e v e l o p, etc.)

Thanks,
bobo
bowman@reed.{bitnet,UUCP,edu}
...!tektronix!reed!bowman

anders@verity.com (Anders Wallgren) (01/15/91)

In article <15876@reed.UUCP>, bowman@reed (Eric Bowman) writes:
>Has someone, somewhere, sometime elucidated when to lock down a handle
>object? (i.e. calling constructors, virtual functions, etc.)
>

develop, issue 2, has an article by Curt Bianchi called "Using Objects
Safely in Object Pascal."  I've found that most of that article
applies to C++ as well, and since reading it I can't recall having any
problems with loose handles, and my income has more than tripled and
my health improved greatly.  Call now!

anders

marc@Apple.COM (Mark Dawson) (01/15/91)

In article <15876@reed.UUCP> bowman@reed.UUCP (Eric Bowman) writes:
>Has someone, somewhere, sometime elucidated when to lock down a handle
>object? (i.e. calling constructors, virtual functions, etc.)
>
>Currently I'm proceeding somewhat by trial and error (with success...apparently)
>but it seems like a sticky point.
>
>Any references appreciated (d e v e l o p, etc.)
>
I generally try to lock down a handle object when I'm calling procedures/
functions that use pointers to my object.  For example, if you have an
array x (Str255 x):
  GetName(this->x);

  Now if GetName(Str255 x) does something like this:
   char *z = new 2;
   x[0] = sprintf((char *)&x[1],"Hello world");

You could be in trouble--MPW C++ pushes the address of the array (x) onto the
stack; after the new (or any toolbox call that can move memory) "this" may
be moved, moving the address of the array "x".  So now when you do the
"x[0] = sprintf(..." statement, "x"'s address is no longer valid.  The
correct procedure would be to:
  HLock((Handle)this);
  GetName(this->x);
  HUnlock((Handle)this);

I ran across this when I stress tested my own (MacApp/C++ based ) program
using MacsBugs heap scrambling commands ("HS;ATHCA").

Hope this helps.
   Mark

 



-- 
---------------------------------
Mark Dawson                Service Diagnostic Engineering
AppleLink: Dawson.M

Apple says what it says; I say what I say.  We're different
---------------------------------