[comp.sys.mac.programmer] Unsafe use of an object field as a var or > 4 byte parameter

paulr@syma.sussex.ac.uk (Paul Russell) (03/19/91)

When passing a parameter such as a Str255 as a parameter
to a method, where the parameter is also an object field,
the MPW Pascal compiler complains:

### Pascal - Error 815 Unsafe use of an object field as a var or > 4 byte parameter

I can understand why it might object to a VAR parameter being
passed in this way, since that would in effect be dereferencing
a handle. But why should it object when the parameter is not
VAR ? For non-VAR parameters surely a local copy of the parameter
is made before the called function has a chance to move memory ?
Am I missing something here ? It's a real pain to have to keep
making temporary copies of object fields before passing them to
methods but I don't want to start putting {$H-} directives in the
code unless I know it really is safe.

(BTW - THINK Pascal doesn't care about this - does that mean that
it handles the problem for you, or that it just ignores it ? ;-))

//Paul

unierik@uts.uni-c.dk (Erik Bertelsen) (03/21/91)

When calling a procedure with a non-VAR parameter larger than 4 bytes,
the address of the value is pushed on the stack and the entry code within the
procedure makes a copy of the parameter when the procedure is called.
This is the problem if the procedure call causes a segment load which
relocates the object whose field is passed as a parameter.

{$push}{$h-}  doit {$pop} is safe when the called procedure is in the same
segment as the caller or in a resident segment.

Regards
Erik Bertelsen
UNI-C, The Danish Computing Centre for Research and Education.

tim@hoptoad.uucp (Tim Maroney) (03/23/91)

In article <4749@syma.sussex.ac.uk> paulr@syma.sussex.ac.uk (Paul Russell)
writes:
>When passing a parameter such as a Str255 as a parameter
>to a method, where the parameter is also an object field,
>the MPW Pascal compiler complains:
>
>### Pascal - Error 815 Unsafe use of an object field as a var or > 4 byte parameter
>
>I can understand why it might object to a VAR parameter being
>passed in this way, since that would in effect be dereferencing
>a handle. But why should it object when the parameter is not
>VAR ? For non-VAR parameters surely a local copy of the parameter
>is made before the called function has a chance to move memory ?

Nope.  A pointer is passed.  It's the responsibility of the called
routine, not the caller, to copy it.  This is likely the first thing
the called routine does.  So why is it unsafe?  The routine may be in
another segment, so the routine call will turn into a LoadSeg
instruction, which may move memory.

>(BTW - THINK Pascal doesn't care about this - does that mean that
>it handles the problem for you, or that it just ignores it ? ;-))

It just ignores the problem.  But you can still get bitten.
-- 
Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com

FROM THE FOOL FILE:
"Yet another piece of evidence that it's a Communist society which is being
 presented as good, but which we probably would not want to live in."
	-- Ken Arromdee on rec.arts.startrek, on the Federation's Red Menace